Activating a Workbook with a changing name

JFlagg

New Member
Joined
Sep 29, 2019
Messages
1
Hi,

I'm new to VBA, I'm recording a macro to format a specific file that a team uses to work through a que, I'm trying to get my macro to reference the workbook regardless of the changing number in the file. This name change is caused by the way the file downloads in certain browsers i.e chrome and edge vs IE, currently it looks like this:

Windows("Recalc.xls").Activate
The various names that it could need to reference would be:
Windows("Recalc (1).xls").Activate
Windows("Recalc (2).xls").Activate....

The sheet name also changed per the workbook name and I'd have to reference that changing sheet name at some point too. I've paste part of what the recorder generated below.

Windows("Recalc.xls").Activate
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFilter
Range("A2").Select
ActiveWorkbook.Worksheets("Recalc").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Recalc").Sort.SortFields.Add(Range("F3:F299"), _
xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 0 _
, 0)
ActiveWorkbook.Worksheets("Recalc").Sort.SortFields.Add(Range("F3:F299"), _
xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, _
255, 0)

Also this macro is split into several subs for some reason, the person who originally recorded it left the company though. I'm wondering if there is a way to set a variable for the name that could change in each sub.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi. Im presuming the sheet is named by the same name as the workbook. The following shows how you could get both the workbook and the worksheet into two variables, namely wb and ws.

Code:
Dim wb As Workbook, wrkbk As Workbook, sh As Worksheet

For Each wrkbk In Workbooks
    If wrkbk.Name Like "Recalc*" Then
        Set wb = wrkbk
        Set sh = wb.Sheets(Split(wrkbk.Name, ".")(0))
    End If
Next
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,245
Members
448,555
Latest member
RobertJones1986

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