Assign w.Name to variable

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
367
Office Version
  1. 2019
A user of my macro can have multiple files open. For three of these files I know roughly what these files will be called, but as the name changes slightly each time the macro is run, I need to use variables when working with the files.
I've found an old thread that used this code:
VBA Code:
Sub test()
For Each w In Workbooks
If w.Name Like "*View Repair Inspection*" Then
Windows(w.Name).Activate
Exit For
End If
Next w

End Sub

I have attempted to adapt it as follows:
VBA Code:
Dim w As Workbook, ExpReport As Workbook, Xero As Workbook, TypeReport As Workbook

For Each w In Workbooks
    If w.Name Like "*Exportier*" Then
        ExpReport = Windows(w.Name).Name
        ExpReport = w.Name
        Exit For
    End If
Next w
When I hover over w.Name, the code has successfully identified the correct file, but neither of my attempts above at setting the ExpReport variable to the workbook name are working.
How can I set ExpReport to something useful, so in further code I can just refer to it like LastRow = ExpReport.ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try:
VBA Code:
Dim w As Workbook, ExpReport As Workbook

For Each w In Workbooks
    If w.Name Like "*Exportier*" Then
        Set ExpReport = w 
        Exit For
    End If
Next w

LastRow = ExpReport.ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
 
Upvote 0
Solution

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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