Need a second Myname value for second workbook

Bill_Biggs

Well-known Member
Joined
Feb 6, 2007
Messages
1,216
I have a macro has to move between two workbooks. In the first workbook when the macro is activted, I have set up the workbook know its name with the following:

Myname = ThisWorkbook.Name
Q = Myname

When the second macro is opened, I need the macro to remember the second workbooks name. Initially, I have it open the workbook with the following:

Filename = Application.GetOpenFilename("XLS Files(*.xls),", , "Select Latest Received File")
If Filename = False Then
MsgBox "No file was selected"
Exit Sub
End If

A = Filename
Application.ScreenUpdating = True
Workbooks.Open Filename:=A

But now I need the macro to create a DIM with only the workbook name as Q is set up above. I tried assigning a second myname value (D=myname2) here but it simply defaults to the first myname value, thuw not repoening the swecond book when commanded. How do I get the macro to remember and recall the second workbook name without closing and reopening it?

Thanks,

Bill
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
Bill

Why not make life a little easier and create a reference to the workbook you open?
Code:
Set wbOpen = Workbooks.Open (Filename:=A)
By the way what is the code actually doing that you need to switch between workbooks?

PS Oh, and I would recommend you use something a little more descriptive for your variable names.:)
 
Upvote 0

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
When workbook 2 is opens it then becomes the activeworkbook, so you can refer to it with MyName2 = ActiveWorkbook.Name.

Also note that you should be able to condense your code a bit:

Code:
            If Filename = False Then
                MsgBox "No file was selected"
                Exit Sub
            Else: Workbooks.Open Filename
            End If

HTH,
 
Upvote 0

Forum statistics

Threads
1,190,885
Messages
5,983,395
Members
439,842
Latest member
grammenmg5

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
Top