Any way to fix it?
Renaming duplicate column names
- SUBHASHn
- Thread is marked as Resolved.
-
-
-
Do you have any blank cells in column A?
-
In original file the data is in column B so changed it accordingly as below.
-
No blank cells
-
I tried the macro on the file you posted after moving the data to column B, and it worked properly. The error you got indicates that there is no filtered data to analyze. This makes no sense because the filter criteria is taken from the array which was created using the existing data in column B so there should always be data visible after the filtering is done. The only thing I can suggest is that you post a copy of your actual file, de-sensitized if necessary) so that I can test the macro on the actual data.
-
-
Can I skip 6-11 lines of the code as it is taking lot of time and the excel I have has 80k rows
I see you've moved on , but this is a reply to post #15. It may be a little quicker...
Code
Display MoreSub Rename_v2() Dim lRow As Long, rg As Range Dim v As Variant, k As Variant Dim i As Long, j As Long, x As Long Application.ScreenUpdating = False With Sheet1 lRow = .Cells(Rows.Count, 7).End(xlUp).Row Set rg = Sheet1.Range("g2:g" & lRow) v = Application.Transpose(rg) With CreateObject("Scripting.Dictionary") For Each k In v .Item(k) = Empty Next k k = .keys End With v = rg.Resize(, 2) For i = LBound(k) To UBound(k) x = 0 For j = LBound(v) To UBound(v) If k(i) = v(j, 1) Then x = x + 1 v(j, 2) = Replace(k(i), ".", x & ".") End If Next j Next i End With rg.Resize(, 2) = v Application.ScreenUpdating = True End Sub
-
One last question @ Mumps will it impact the code if there is some data in column A?
-
No, it shouldn't.
-
Hi @ dangelor thanks for the code if there is any duplicate it has to add _1.pdf,_2.pdf to it...in the above code it is adding A11.pdf,A22.pdf it should be like A1_1.pdf and A2_2.pdf
-
Added the dash...
Code
Display MoreSub Rename_v2() Dim lRow As Long, rg As Range Dim v As Variant, k As Variant Dim i As Long, j As Long, x As Long Application.ScreenUpdating = False lRow = Sheet1.Cells(Rows.Count, 7).End(xlUp).Row Set rg = Sheet1.Range("g2:g" & lRow) v = Application.Transpose(rg) With CreateObject("Scripting.Dictionary") For Each k In v .Item(k) = Empty Next k k = .keys End With v = rg.Resize(, 2) For i = LBound(k) To UBound(k) x = 0 For j = LBound(v) To UBound(v) If k(i) = v(j, 1) Then x = x + 1 v(j, 2) = Replace(k(i), ".", "_" & x & ".") End If Next j Next i rg.Resize(, 2) = v Application.ScreenUpdating = True End Sub
-
-
Hi dangelor it adding add " _" for all the cells...it should only add it to the duplicates..In the below I have highlighted the duplicate with yellow which is correct if there is no duplicate it should keep the same
-
Here it is adding+1 to all the cells
-
it should be like this
-
Try...
Code
Display MoreSub Rename_v3() Dim lRow As Long, rg As Range Dim v As Variant, k As Variant Dim i As Long, j As Long, x As Long Application.ScreenUpdating = False lRow = Sheet1.Cells(Rows.Count, 7).End(xlUp).Row Set rg = Sheet1.Range("g2:g" & lRow) v = Application.Transpose(rg) With CreateObject("Scripting.Dictionary") For Each k In v .Item(k) = Empty Next k k = .keys End With v = rg.Resize(, 2) For i = LBound(k) To UBound(k) x = 0 For j = LBound(v) To UBound(v) If k(i) = v(j, 1) Then x = x + 1 If x > 1 Then v(j, 2) = Replace(k(i), ".", "_" & x - 1 & ".") Else v(j, 2) = k(i) End If End If Next j Next i rg.Resize(, 2) = v Application.ScreenUpdating = True End Sub
-
Hi again, the actual data is in column B and I changed 7 to 2 and g2:g to b2:b but Iam getting this error.
-
-
What error and on which line of code?
-
Here I am opening the actual file from seperate macro file...the error is highlighted here
-
...and the error is???
-
Try this version...
Code
Display MoreSub Rename_v5() Dim lRow As Long, rg As Range Dim v As Variant, k As Variant Dim i As Long, j As Long, x As Long Application.ScreenUpdating = False With Workbooks.Open(Filename:="C:\BP\Input.xlsx").Worksheets("Sheet1") '''Change worksheet name as needed''' lRow = .Cells(Rows.Count, 1).End(xlUp).Row Set rg = .Range("a2:a" & lRow) End With v = Application.Transpose(rg) With CreateObject("Scripting.Dictionary") For Each k In v .Item(k) = Empty Next k k = .keys End With v = rg.Resize(, 2) For i = LBound(k) To UBound(k) x = 0 For j = LBound(v) To UBound(v) If k(i) = v(j, 1) Then x = x + 1 If x > 1 Then v(j, 2) = Replace(k(i), ".", "_" & x - 1 & ".") Else v(j, 2) = k(i) End If End If Next j Next i rg.Resize(, 2) = v Application.ScreenUpdating = True End Sub
-
To save the workbook what should be the line of code before End sub?
-
Participate now!
Don’t have an account yet? Register yourself now and be a part of our community!