Can a pause be placed in code?

GiraffetheGeek

Board Regular
Joined
May 25, 2011
Messages
58
Hi there,

I have a button in a spreadsheet.

What I want is the when the button is pressed it's code is run to a certain point which will open another spreadsheet. I want it to then pause and wait until the other spreadsheet is closed before restaring and completing.

Is this possible?

This is what I have so far
Code:
Private Sub Btn_Budgetors_Click()
    Workbooks.Open Filename:= "file.xls"
    MsgBox ("Remember to update Service Plan")
    ActiveWorkbook.Save
End Sub

So I want it to run the first line, and then wait until the other spreadsheet is closed before running the last two lines.

Cheers in advance for your help.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
with some code from other places it would look something like this:

Code:
''''''''''''''''''''''''''''''''''''''''''
'Written by www.ozgrid.com
'Test to see if a Workbook is open.
''''''''''''''''''''''''''''''''''''''''''
Dim wBook As Workbook
    On Error Resume Next
Recheck:
    Set wBook = Workbooks("file.xls")
        If wBook Is Nothing Then 'Not open
            
            Set wBook = Nothing
            On Error GoTo 0
        else
            Application.Wait Time + TimeSerial(0, 0, 5)
            goto Recheck
        End If
 
Upvote 0
What would the user be doing during this pause? Entering into the worksheet or sipping coffee (or some other non-Excel activity)?
 
Upvote 0
Well with the little bit of code you have there this should do the trick.
I have not fully tested it, it is a bit of code that I use in one of my own spreadsheets, modified for your use.
Code:
Private Sub Btn_Budgetors_Click()     
     Workbooks.Open Filename:= "file.xls"
'''''''''''''''''''''''''''''''''''''''''' 
'Written by www.ozgrid.com 
'Test to see if a Workbook is open. 
'slightly modified to do other stuff
'''''''''''''''''''''''''''''''''''''''''' 
Dim wBook As Workbook     
On Error Resume Next 
Recheck:     
     Set wBook = Workbooks("file.xls")         
If wBook Is Nothing Then 'Not open
             Set wBook = Nothing
             On Error GoTo 0        
else
             Application.Wait Time + TimeSerial(0, 0, 5)
             goto Recheck         
End If
'''''''''''''''''''''''''''''''''''''''
'End of the Ozgrid and my own code
'''''''''''''''''''''''''''''''''''''''
MsgBox ("Remember to update Service Plan")     
ActiveWorkbook.Save 
End Sub
 
Upvote 0
OK I finally had some time to test that and it didn't work the way I expected.

I'm sorry. First bad advice I have given in a long time. I'll see if I can work something up for you.
 
Upvote 0
The only way I could figure out how to do what you want was to use a custom userform, set it modal = false with an OK button and a message about not clicking until after changing the workbook.

Code:
Private Sub Btn_Budgetors_Click()
    Workbooks.Open Filename:= "file.xls"
    CustomUserform.show 
 '' MsgBox ("Remember to update Service Plan")
    ActiveWorkbook.Save 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,303
Members
452,904
Latest member
CodeMasterX

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