opening a workbook with a command button

MartinL

Well-known Member
Joined
Oct 16, 2008
Messages
1,141
Office Version
  1. 365
Platform
  1. Windows
Hi

I have been looking at summing closed workbooks here and decided that due to the problems people are facing I would be better off opening the workbooks

I have a command button that makes the cells switch between 2 sets of data.

The second set of data is held in a second workbook and needs to be recalculated every time. Works fine if the workbook is open but only returns the saved values if closed.

If have added some VBA to the Command Button that opens the 2nd spreadsheet, however, is there a way to get it to open in the background rather than on top of the sheet I am working on.

My humble approach has been to write a macro and then edit it to go back to the original sheet I have been working like so:

Private Sub CommandButton1_Click()
'
' OpenABC Macro
' Macro recorded 21/04/2009 by martin.lucas
'
' Keyboard Shortcut: Ctrl+Shift+M
'
ChDir "L:\Customers\ABC\ABC Stats"
Workbooks.Open Filename:="L:\Customers\ABC\ABC Stats\MasterABC.xls", _
UpdateLinks:=0
Windows("TopLine Management.xls").Activate

End Sub


Can this be done without the user knowing it has happened.
and can a check be done so that excel does not try to open it again if it is already open.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You can always hide the workbook....

Code:
Private Sub CommandButton1_Click()
'
' OpenABC Macro
' Macro recorded 21/04/2009 by martin.lucas
'
' Keyboard Shortcut: Ctrl+Shift+M
'
ChDir "L:\Customers\ABC\ABC Stats"
Workbooks.Open Filename:="L:\Customers\ABC\ABC Stats\MasterABC.xls", _
UpdateLinks:=0

ActiveWindow.Visible = False


End Sub
 
Upvote 0
Hi

Thanks for the quick reply, Sorry I didnt answer earlier I had a client meeting.
Your suggestion was quite interesting as after I press the command button you can see the spreadsheet open but then it appears to close but the data updates fantastically each time I change the criteria.

However if the button is pressed again it tries to open the spreadsheet again and a message box appears to warn you. Can this be checked for in the VBA: if ABC.xls is open then do nothing.....that sort of thing?
 
Upvote 0
Hi

I am trying to calculate values from these three closed spreadsheets.
I have added this to my spreadsheet but it does not appear to work.
What I mean is, I still have to physically open them before my spreadsheet will populate until then I get #N/A

In Edit\Edit links\Startup Prompt I have set don't display the alert and update Links
I am assuming this means update all links but don't bother telling me

And in Options\Calculation I have set automatic calculation ON

Private Sub Worksheet_Activate()
' OpenLinkedDB Macro
' Macro recorded 21/04/2009 by martin.lucas
'
' Open Special Retailer1
'
ChDir "L:\Customers\A&B\A&B Stats"
Workbooks.Open Filename:="DatabaseA&B.xls", _
UpdateLinks:=1
ActiveWindow.Visible = False

' OpenPremiumdb
ChDir ":\Performance Data\Market Data"
Workbooks.Open Filename:="Premium_Report.xls", _
UpdateLinks:=1
ActiveWindow.Visible = False

' OpenPrepareddb
ChDir ":\Performance Data\TNS Market Data"
Workbooks.Open Filename:="Prepared_Report.xls", _
UpdateLinks:=1
ActiveWindow.Visible = False

End Sub


Is there a problem with what I am trying to do ie open 3 or more spreadsheets in this fashion
 
Last edited:
Upvote 0
I see from a previous thread that yopu can only open 1 workbook with this method. Is there a way around this as I need to open 3 now and possibly 4
I think one workbook is opening as some of my values appear to be updating.

Oh my head!
 
Upvote 0
I use this to update links in my workbooks instead of having them open and then hide in the background. But I only have one sheet to link to. Might b able to double ud the update link for multiple sheets. IF not let me know so I dont scratch my head later lol......


Private Sub Worksheet_Change(ByVal Target As Range)
'Adjust Range(" : ") according to needs
If Not Intersect(Target, Range(" ")) Is Nothing Then
ActiveWorkbook.UpdateLink Name:= _
" your file path.xlsx", Type:= _xlExcelLinks
End If
End Sub
 
Upvote 0
Why not just turn off ScreenUpdating, open the file(s), do your thing, close the file(s) and turn ScreenUpdating back on?
Rich (BB code):
Rich (BB code):
Application.ScreenUpdating = False
 
ChDir "L:\Customers\A&B\A&B Stats"
 
Set wb1 = Workbooks.Open (Filename:="DatabaseA&B.xls", UpdateLinks:=1)
 
' OpenPremiumdb
ChDir ":\Performance Data\Market Data"
Set wb2 = Workbooks.Open (Filename:="Premium_Report.xls", UpdateLinks:=1)
 
' OpenPrepareddb
ChDir ":\Performance Data\TNS Market Data"
Set wb3 = Workbooks.Open (Filename:="Prepared_Report.xls", UpdateLinks:=1)
 
' you should now have the 3 workbooks open and you can reference them using wb1-3, but I would recommend you use more descriptive names
 
 
' run code
 
' close workbooks
 
wb1.Close
wb2.Close
wb3.Close
 
Application.Screenupdating = True
 
Upvote 0
Hi
Type:= _xlExcelLinks

I think this is a 2007 thing
I'm using 2003 :eek:(

Thanks for replying though
Since your option I've also looked at

ActiveWorkbook.UpdateRemoteReferences
and ActiveWorkbook.UpdateLinks

but have no idea how these may work
My Mr Walkenbach does not seem to cover them in my old 2002 manuals time.
Methinks its time to get a new set of books
 
Upvote 0
Martin

xlExcelLinks is a VBA constant equal to 2, it's available at least as far back as Excel 2000.:)
 
Upvote 0
Hi Norie

I have added your code to a dropdown box
The theory being that when a change is made the code runs

For test reasons I used just one workbook so my code looks like this

Application.ScreenUpdating = False

ChDir "L:\Customers\A&B\A&B Stats"

Set wb1 = Workbooks.Open (Filename:="DatabaseA&B.xls", UpdateLinks:=1)


' you should now have the 3 workbooks open and you can reference them using wb1-3, but I would recommend you use more descriptive names


' run code

' close workbooks

wb1.Close

Application.Screenupdating = True
However it has brought my spreadsheet to a standstill.
after 20 mins its still not responding
 
Upvote 0

Forum statistics

Threads
1,214,958
Messages
6,122,475
Members
449,087
Latest member
RExcelSearch

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