VBA Change Data Type of Excel Table Columns

Bubna, Shreya 61 Reputation points
2021-03-06T22:34:10.917+00:00

Hello,

I have an Excel Table called "ABC" with a number of Percent Columns "GH%" , "AD%" etc.

I want to write a code which would change the Data type of these Percent Columns which have "%" in their Column headers to type Percentage.

Kindly help with this. Thank you.

Developer technologies Visual Basic for Applications
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2021-03-07T07:30:05.777+00:00

    Try this code:

    Dim ws As Worksheet
    Set ws = Sheet1 ' TODO: use the correct worksheet
    
    Dim t As ListObject
    Set t = ws.ListObjects("ABC") ' TODO: use the correct table name
    
    Dim c0
    For Each c0 In t.ListColumns
        Dim c As ListColumn
        Set c = c0
    
        If Right(c.Name, 1) = "%" Then
            Dim r As Range
            Set r = c.DataBodyRange
            r.NumberFormat = "0""%"""
            ' or maybe r.NumberFormat = "0%"
        End If
    Next
    

    Adjust it according to your needs. Backup the file before experimenting.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.