VBA to display a different sheet before displaying a message box

Derek

Well-known Member
Joined
Feb 16, 2002
Messages
1,595
Hi Everyone

I am running an event macro from sheet A and I want to display sheet B on the screen before a message box displays. However the message box always displays first and Sheet B displays after I click OK.
I have tried Doevents, application.screenupdating = true, sheet.activate etc but none of this works for me.

Please can someone tell me the correct VBA to display sheet B before the message box opens.

I really appreciate your help

thanks

Derek
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
seems odd, it should just run in order.

if you have this line just before msgbox worksheets("SheetB").activate I don't see how msgbox could appear first. can you post the events macro?
 
Upvote 0
Hi Dryver14

Yes I already tried active but no joy.

Here is the whole event macro code:

'LOCATE STAFF NUMBER IN SHEET ALL STAFF AND ENTER DATA
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Cells.Count <> 1 Then Exit Sub
If Target.Column <> 6 Then Exit Sub
Sheets("ALL STAFF").Range("F10").Value = Target.Value
Sheets("ALL STAFF").Select
Sheets("ALL STAFF").Range("G10").Select
Dim X As Long
Dim Y As Long
Dim Z As Long
Dim C As Long
Z = Target.Offset(0, 2).Value
X = WorksheetFunction.Match(Target.Value, Sheets("ALL STAFF").Range("H11:H20000"), 0) + 10
Y = WorksheetFunction.CountIf(Sheets("ALL STAFF").Range("N8:XFD8"), "<" & Z)
C = (Y + 14) * 1

ANSWER = MsgBox("Staff Number belongs to " & Sheets("ALL STAFF").Range("F" & X).Value & " " & Sheets("ALL STAFF").Range("G" & X).Value & vbCr & vbCr & "DO YOU WANT TO TERMINATE THIS PERSON?", vbYesNo)
If ANSWER = vbNo Then
Sheets("ALL STAFF").Select
GoTo Here
End If
If ANSWER = vbYes Then
Sheets("ALL STAFF").Range("F" & X & ":G" & X).Copy Destination:=Target.Offset(0, -2)
Application.CutCopyMode = False
Sheets("ALL STAFF").Range("J" & X).Copy Destination:=Target.Offset(0, 1)
Application.CutCopyMode = False
Sheets("ALL STAFF").Range("K" & X).Value = Target.Offset(0, 2).Value

Sheets("ALL STAFF").Select
Sheets("ALL STAFF").Cells(X, C).Select
ActiveWindow.ScrollColumn = C - 5
ActiveWindow.ScrollRow = X
Exit Sub
End If
Here:
Sheets("ALL STAFF").Select
MsgBox "Check this is the correct staff number, and start again"
Exit Sub
End Sub

I have also tried application.screenupdating = true and Doevents

thanks

Derek
 
Upvote 0
move the Sheets("ALL STAFF").select line above the ANSWER line, or copy it, it will make the second instance redundant but wont break anything
 
Upvote 0
Just what sheet are you on? everything seems to be happening on sheet ALL STAFF which you seem to select every 10 lines.

is that the sheet you want to go to?
 
Upvote 0
Hi Again

Found a solution! Need to run it in a new module, as follows:

Change event macro code to this

Here:
Sheets("ALL STAFF").Select
Application.ScreenUpdating = True
Application.EnableEvents = True
MsgBox "Check this is the correct staff number, and start again"
End Sub

but it actually seems to run more smoothly if I put that code in a module macro and just call it.

Thanks to everybody who replied.

Derek
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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