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

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Instead of a pause in one routine, you could use two routines.

Code:
Public runPartTwo as Boolean

Sub Part1()
    Rem do some stuff

    runPartTwo = True
    Workbooks("otherBook.xls").Activate
End Sub

Sub PartTwo()
    Rem do more stuff
End Sub
Then in the Workbook1.xls Workbook_Activate event you would have the code.
Code:
Private Sub Workbook_Activate()
    If runPartTwo Then Call PartTwo
    runPartTwo = False
Exit Sub

Thus, the user would press the button, which calls Part1.
Stuff is done and when it gets to that point, the Public variable runPartTwo is set to True, otherBook is activated and the sub ends.
The user does stuff in otherBook and returns to Workbook1.
The Activate event triggers, running the code which sees that runPartTwo is True so the Sub PartTwo is run, finally runPartTwo is re-set to False and everything is over.
 
Upvote 0
Thanks Mike,

I think I follow that. I may not have a chance to look at it for a little while but I will post once I have tried it.
 
Upvote 0
The other thought that I have depends on what the user is supposed to be doing during the pause.

If the idea is "pause so the user can fill in these specific cells in the other workbook" or "pause so that the user can do one or some of this list of actions in the other workbook" it might be smoother for WB1 to have a userform that requests/inputs those values or allows the user to choose which actions to perform and does them to the other workbook.
Calling that userform could replace the pause.

If the pause is for less structured activity in the other workbook, the two part plan would be what I recommend. (Upon consideration, I think the "work only on OtherBook until Closed and returned to WB1" would be easiest mediated by Application level events rather than Workbook level events in individual workbooks.)
 
Upvote 0
Cheers,

I had thought about the userform too, however I am still a bit of a beginner with VBA and excel.

The other complication is the first workbook will be a shared workbook.

I am working through a number of solutions but just thought there may have been a command that could have been used to tell the code to wait till a specific event.

I did some C++ programming about 10 years ago and remember there being something like that in there.

Still it's all good and I'm sure I'll be able to use some of your solutions.
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,331
Members
452,907
Latest member
Roland Deschain

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