ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Toggle between two workbooks via MACRO (https://www.excelbanter.com/excel-discussion-misc-queries/194082-toggle-between-two-workbooks-via-macro.html)

Aligi

Toggle between two workbooks via MACRO
 
I have two workbooks open, one I know the name and it is where I am running
the macro from, the second one I only now that it is open but I may not know
the name (as it could have been renamed). I need to go to the second workbook
(and make it active) via a MACRO. Thank you in advanced

Aligi

Harald Staff

Toggle between two workbooks via MACRO
 
Hi Aligi

Simplified (does not deal with third or more workbooks, implement that in
the For-Next loop):

Sub test()
Dim Wb As Workbook
Dim Other As Workbook
Dim Home As Workbook

Set Home = ThisWorkbook
For Each Wb In Workbooks
If Wb.FullName < Home.FullName Then
Set Other = Wb
Exit For
End If
Next
If Other Is Nothing Then Exit Sub

Home.Activate
MsgBox Home.FullName
Other.Activate
MsgBox Other.FullName

If MsgBox("Home again?", vbYesNo + vbQuestion) = _
vbYes Then
Home.Activate
MsgBox Home.FullName
Else
Other.Activate
MsgBox Other.FullName
End If

End Sub

HTH. Best wishes Harald

"Aligi" skrev i melding
...
I have two workbooks open, one I know the name and it is where I am running
the macro from, the second one I only now that it is open but I may not
know
the name (as it could have been renamed). I need to go to the second
workbook
(and make it active) via a MACRO. Thank you in advanced

Aligi




Aligi

Toggle between two workbooks via MACRO
 
WOW! That's perfect!!! Thank you.
--
Aligi


"Harald Staff" wrote:

Hi Aligi

Simplified (does not deal with third or more workbooks, implement that in
the For-Next loop):

Sub test()
Dim Wb As Workbook
Dim Other As Workbook
Dim Home As Workbook

Set Home = ThisWorkbook
For Each Wb In Workbooks
If Wb.FullName < Home.FullName Then
Set Other = Wb
Exit For
End If
Next
If Other Is Nothing Then Exit Sub

Home.Activate
MsgBox Home.FullName
Other.Activate
MsgBox Other.FullName

If MsgBox("Home again?", vbYesNo + vbQuestion) = _
vbYes Then
Home.Activate
MsgBox Home.FullName
Else
Other.Activate
MsgBox Other.FullName
End If

End Sub

HTH. Best wishes Harald

"Aligi" skrev i melding
...
I have two workbooks open, one I know the name and it is where I am running
the macro from, the second one I only now that it is open but I may not
know
the name (as it could have been renamed). I need to go to the second
workbook
(and make it active) via a MACRO. Thank you in advanced

Aligi






All times are GMT +1. The time now is 08:50 PM.

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