ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Refer to Unknown File Name (https://www.excelbanter.com/excel-programming/365011-refer-unknown-file-name.html)

Lynn

Refer to Unknown File Name
 
I need to switch between 2 open files in a macro. I know the name of one
file but not the other. What code can I use to activate the workbook that I
don't know the name of?

JMB

Refer to Unknown File Name
 
Maybe

Activewindow.ActivateNext

but it wouldn't be my first choice as it depends on there being only two
workbooks open.


Can you use the macros in the first workbook to open the second during
execution? Since you don't know the file name, I assume the user has to open
the second book.

Sub test()
Dim wkbTest As Workbook
Dim varFName As Variant

varFName = Application.GetOpenFilename
If varFName = False Then End

Set wkbTest = Workbooks.Open(Filename:=varFName)

End Sub


Then you can use the object variable, wkbTest (change variable name if you
like) to refer to the second book.

wkbTest.Worksheets("Sheet1").Range("A1").Value = 5




"Lynn" wrote:

I need to switch between 2 open files in a macro. I know the name of one
file but not the other. What code can I use to activate the workbook that I
don't know the name of?



All times are GMT +1. The time now is 01:20 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com