xyz
xyz

Reputation: 2300

Change names of headers in list with list of new header names

I have a project that has dozens of columns and some of the header names need changing whenever a new excel file is generated for the project.

Also the columns are not contiguous and can shift their positions from one excel file to another.

I have googled this and found nothing that does this specifically.

The closest I have come is this that maps a array of names to the first n headers:

i.e

headerValues = Array("Name1", "Name2", "Name3")

newheaderValues = Array("NewName1", "NewName2", "NewName3")

Thanks

Upvotes: 0

Views: 54

Answers (1)

tigeravatar
tigeravatar

Reputation: 26650

Assuming the headers are in row 1:

Sub tgr()

    Dim headerValues As Variant
    Dim newheaderValues As Variant
    Dim i As Long

    headerValues = Array("Name1", "Name2", "Name3")
    newheaderValues = Array("NewName1", "NewName2", "NewName3")

    For i = LBound(headerValues) To UBound(headerValues)
        Rows(1).Replace headerValues(i), newheaderValues(i), xlWhole
    Next i

End Sub

Upvotes: 3

Related Questions