Assign w.Name to variable

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
336
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

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
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,215,432
Messages
6,124,860
Members
449,194
Latest member
HellScout

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