VBA add on query...

James Lomas

New Member
Joined
Oct 4, 2018
Messages
1
Good afternoon - new here so please be gentle as i'm a total novice at VBA coding...

I have a master spreadsheet that draws information from 3 password protected workbooks. In order to open then I have got the following code:

Sub OpenSesame()
Workbooks.Open Filename:="C:\folder\sub folder\file.xlsm", Password:="password"
Workbooks.Open Filename:="C:\folder\sub folder\file2.xlsm", Password:="password"
Workbooks.Open Filename:="C:\folder\sub folder\sub folder 2\file12.xlsm", Password:="password"

End Sub

Which is great - however it opens all three files - is there an additional code that will close the newly opening spreadsheets?!

Thanks
James
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Place the macro in a standard module in your master workbook and run it from there. The macro assumes that only the Master and the other 3 workbooks are open.

Code:
Sub OpenSesame()
    Dim wb As Workbook, desWB As Workbook
    Set desWB = ThisWorkbook
    Workbooks.Open Filename:="C:\folder\sub folder\file.xlsm", Password:="password"
    Workbooks.Open Filename:="C:\folder\sub folder\file2.xlsm", Password:="password"
    Workbooks.Open Filename:="C:\folder\sub folder\sub folder 2\file12.xlsm", Password:="password"
    For Each wb In Workbooks
        If wb.Name <> desWB.Name Then
            wb.Close False
        End If
    Next wb
End Sub
 
Last edited:
Upvote 0
If you want/need to control each workbook individually, one way is to assign them to specific worksheet variables.
That just makes it a bit easier to bounce around from one to the other.
Code:
Sub OpenSesame()
    Dim wb1 As Workbook, wb2 As Workbook, wb3 As Workbook
    Workbooks.Open Filename:="C:\folder\sub folder\file.xlsm", Password:="password"
    Set wb1 = ActiveWorkbook
    Workbooks.Open Filename:="C:\folder\sub folder\file2.xlsm", Password:="password"
    Set wb2 = ActiveWorkbook
    Workbooks.Open Filename:="C:\folder\sub folder\sub folder 2\file12.xlsm", Password:="password"
    Set wb3 = ActiveWorkbook
End Sub
Then you can activate a specific workbook like this:
Code:
wb1.Activate
or close it like this:
Code:
wb1.Close
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

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