VBA - Windows activate (Name discrepancy)

MartinST

Board Regular
Joined
Jun 22, 2010
Messages
101
I have a workbook named outstanding actions. I have several new workbooks that is created from time to time that I use a macro in these new workbooks to copy the action from these workbooks to the Outstanding actions workbook. The problem is that the workbook name change creates a problem in VBA whereby I don’t know how to overcome the problem on how to change the workbook name in VBA. For Example this workbook is named "Spares" and the Workbook selection will be " Windows("Spares.xls").Activate " in VBA what if the Workbook name changes to "Cars". How can this problem be overcome.
VBA must automatically change the name in the code. I have a cell that contains the workbook name, I am sure it could be used but how do you code that?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi

There are a variety of ways you can refer to a workbook - specifically, if the workbook is the one in which the code resides (in a module) you can refer to it via ThisWorkbook:

Code:
ThisWorkbook.Activate

Given that you have the name of the workbook in a cell, you can utilise this thus:

Code:
Workbooks(Workbooks("SomeWorkbook.xls").Worksheets("SomeSheet").Range("A1").Value).Activate

WhereSomeWorkbook.xls is the one containing the sheet which holds the workbook name in A1 that you want to activate
 
Upvote 0
First, create hidden name with the name of the workbook which will be changed.
Code:
Sub CreateHiddenName()

    Dim s As String
    s = "Spares"
    Names.Add Name:="Book", RefersTo:=s, Visible:=False

End Sub

Then you can change it anytime you want.
Code:
Sub UseNameInWorkbooks()

    ' Refer to workbook "Spares"
    Workbooks(Names("Book").Value).Activate
    
    ' Change name.
    Names("Book").Value = "Cars"
    
    ' Refer to workbook "Cars"
    Workbooks(Names("Book").Value).Activate
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,628
Members
452,933
Latest member
patv

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