VBA code to search a specific row (header) for duplicates and modify repeated words??

dougmarkham

Active Member
Joined
Jul 19, 2016
Messages
252
Office Version
  1. 365
Platform
  1. Windows
Hi Folks,

I am processing data in a large table that contains some duplicate column header names e.g., there are 2 headers called "Country", two instances of "Customer" and "Product" etc, etc. I cannot find any instance of this on Google search.

Would you please help with the VBA code?

What it needs to do is search 'row 1' for duplicate header names and rename the second, third, fourth etc occurrences by adding a number representing the occurrence number.

E.g., if Country occurred 3 times in A1, Z1, and AZ1, the VBA would locate the duplicate names and rename the repeats thus...

A1 = Country
Z1 = Country1
AZ1 = Country2

Kind regards,

Doug.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Code:
Public Sub RenameHeaders()

Dim lastCol As Long
Dim thisCol As Long
Dim testCol As Long
Dim foundCount As Long

lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
For thisCol = 1 To lastCol - 1
    If Cells(1, thisCol).Value <> "" Then
        foundCount = 0
        For testCol = thisCol + 1 To lastCol
            If Cells(1, thisCol).Value = Cells(1, testCol).Value Then
                foundCount = foundCount + 1
                Cells(1, testCol).Value = Cells(1, thisCol).Value & CStr(foundCount)
            End If
        Next testCol
    End If
Next thisCol

End Sub

WBD
 
Upvote 0
Code:
Public Sub RenameHeaders()

Dim lastCol As Long
Dim thisCol As Long
Dim testCol As Long
Dim foundCount As Long

lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
For thisCol = 1 To lastCol - 1
    If Cells(1, thisCol).Value <> "" Then
        foundCount = 0
        For testCol = thisCol + 1 To lastCol
            If Cells(1, thisCol).Value = Cells(1, testCol).Value Then
                foundCount = foundCount + 1
                Cells(1, testCol).Value = Cells(1, thisCol).Value & CStr(foundCount)
            End If
        Next testCol
    End If
Next thisCol

End Sub

WBD

Hi WBD,

This did the trick, thank you very much! :)

Kind regards,

Doug.
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top