Macro to remove duplicate workbook names on sheet1 if workbook contains .xls

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have several workbook name extracted to sheet1 that end in .xls or .xlsm


I would like a macro to delete only those rows where the workbook name have been duplicated and contains .xls


Eg M_BR1 TB.xls
M_BR1 TB.xlsm
M_BR2 TB.xls
M_BR3 TB.xls
M_BR3 TB.xlsm


In this example only the row containing M_BR1 TB.xls &M_BR3 TB.xls are to be deleted


Your assistance in this regard is most appreciated
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Maybe this:
Assuming data in col A
Code:
[FONT=lucida console][COLOR=Royalblue]Sub[/COLOR] a1088724a()
[I][COLOR=seagreen]'https://www.mrexcel.com/forum/excel-questions/1088724-macro-remove-duplicate-workbook-names-sheet1-if-workbook-contains-xls.html[/COLOR][/I]
[COLOR=Royalblue]Dim[/COLOR] i [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]
[COLOR=Royalblue]Dim[/COLOR] rg [COLOR=Royalblue]As[/COLOR] Range
[COLOR=Royalblue]Dim[/COLOR] va, vb
[COLOR=Royalblue]Dim[/COLOR] d [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Object[/COLOR]

[COLOR=Royalblue]Set[/COLOR] rg = Range([COLOR=brown]"A1"[/COLOR], Cells(Rows.count, [COLOR=brown]"A"[/COLOR]).[COLOR=Royalblue]End[/COLOR](xlUp))
va = rg
[COLOR=Royalblue]ReDim[/COLOR] vb([COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] UBound(va, [COLOR=crimson]1[/COLOR]), [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=crimson]2[/COLOR])

[COLOR=Royalblue]For[/COLOR] i = [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] UBound(va, [COLOR=crimson]1[/COLOR])
    vb(i, [COLOR=crimson]1[/COLOR]) = Split(va(i, [COLOR=crimson]1[/COLOR]), [COLOR=brown]"."[/COLOR])([COLOR=crimson]0[/COLOR])
    vb(i, [COLOR=crimson]2[/COLOR]) = Split(va(i, [COLOR=crimson]1[/COLOR]), [COLOR=brown]"."[/COLOR])([COLOR=crimson]1[/COLOR])
[COLOR=Royalblue]Next[/COLOR]


[COLOR=Royalblue]Set[/COLOR] d = CreateObject([COLOR=brown]"scripting.dictionary"[/COLOR])
d.CompareMode = vbTextCompare

[COLOR=Royalblue]For[/COLOR] i = [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] UBound(va, [COLOR=crimson]1[/COLOR])
    [COLOR=Royalblue]If[/COLOR] d.Exists(vb(i, [COLOR=crimson]1[/COLOR])) [COLOR=Royalblue]Then[/COLOR]
    d(vb(i, [COLOR=crimson]1[/COLOR])) = [COLOR=crimson]2[/COLOR]
    [COLOR=Royalblue]Else[/COLOR]
    d(vb(i, [COLOR=crimson]1[/COLOR])) = [COLOR=crimson]1[/COLOR]
    [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
[COLOR=Royalblue]Next[/COLOR]

[COLOR=Royalblue]For[/COLOR] i = [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] UBound(vb, [COLOR=crimson]1[/COLOR])
x = vb(i, [COLOR=crimson]1[/COLOR])
        [COLOR=Royalblue]If[/COLOR] d.Exists(x) [COLOR=Royalblue]And[/COLOR] d(x) = [COLOR=crimson]2[/COLOR] [COLOR=Royalblue]And[/COLOR] LCase(vb(i, [COLOR=crimson]2[/COLOR])) = [COLOR=brown]"xls"[/COLOR] [COLOR=Royalblue]Then[/COLOR]
        Range([COLOR=brown]"A"[/COLOR] & i) = [COLOR=brown]"FALSE"[/COLOR]
        [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
[COLOR=Royalblue]Next[/COLOR]
rg.SpecialCells(xlConstants, xlLogical).EntireRow.Delete

[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR][/FONT]
 
Last edited:
Upvote 0
A macro can be created from the macro recorder. Assuming the names are in column A :
• Sort column A (Z-A)
• Insert a column at column A
• Copy column B and paste to column A
• Select column A
• Find ".xlsm", Replace ""
• Find ".xls",Replace ""
• Select columns A:B
• Remove Duplicates (Data Tools) - column A
• Delete column A
 
Last edited:
Upvote 0
Hi Akuini


Thanks for the help. I get a compile error : Variance not defined

See below


Code:
 x = vb(i, 1)


Kindly test & amend code
 
Upvote 0
Hi Akuini


Thanks for the help. I get a compile error : Variance not defined

See below


Code:
 x = vb(i, 1)


Kindly test & amend code

Sorry, forget to declare variable x.
Just add 1 line:

Code:
Dim x As String

in the declare section
 
Upvote 0
Thanks for update. It now works perfectly
 
Upvote 0
You're welcome, glad to help, & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

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