Modify Macro to use FDIALOG to select Workbook 2 in Excel

adibakale

Board Regular
Joined
Apr 10, 2015
Messages
52
I have a macro that compares 2 excel files and highlights duplicates in Column A of both workbooks.

I want to run the macro from workbook 1, then use fdialog too choose Workbook 2 that i want to compare and highlight duplicates.

I am fairly new to VBA and cannot figure this out, Any help is greatly appreciated.



Sub hiLiteDups()
Dim wb1 As Workbook, wb2 As Workbook, sh1 As Worksheet, sh2 As Worksheet, lr As Long, c As Range, fn As Range, fAdr As String
Set wb1 = Workbooks(1) 'This will be the host for the macro and should be opened first
Set wb2 = Workbooks(2) 'This will be the target workbook and should be opened second.
'as an alternative to opening in a certain sequence, you could use the workbook name instead of the index numbers.
Set sh1 = wb1.Sheets(1) 'Edit sheet name
Set sh2 = wb2.Sheets(1) 'Edit sheet name
lr = sh1.Cells(Rows.Count, 1).End(xlUp).Row
For Each c In sh1.Range("A2:A" & lr)
Set fn = sh2.Range("A:A").Find(c.Value, , xlValues, xlWhole)
If Not fn Is Nothing Then
fAdr = fn.Address
Do
c.Interior.ColorIndex = 3
fn.Interior.ColorIndex = 3
Set fn = sh2.Range("A:A").FindNext(fn)
Loop While fn.Address <> fAdr
End If
Next
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Good question...

From the looks of your code, I think you just need the extra bit below to see how to pull the filename with the dialog and then assign it to the WB2 object:

Code:
Sub PickaFile()

    Dim MyPick As String
    With Application.FileDialog(msoFileDialogFilePicker)
        .Show
        MyPick = .SelectedItems(1)
    End With
    

    Dim WB2 As Workbook
    Set WB2 = Workbooks.Open(MyPick)
    

End Sub
 
Upvote 0

Forum statistics

Threads
1,203,356
Messages
6,054,932
Members
444,759
Latest member
TeckTeck

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