More MsgBox questions.

P

Pete

I am attempting to use the Msgbox function with the Ok/Cancel buttons. the
code I have written thus far is below:

Msgbox("Message",1)

If ans = vbOk then call [subroutine]
If ans = vbCancel the exit sub

end if

end sub

The subroutine does not work when the "Ok" button is used. I have tried
other variations on this to no avail. Could anyone please assist?
 
J

Jacob Skaria

Try one of these

Sub Macro1()
If MsgBox("Message", 1) <> vbOK Then Exit Sub
Call subroutine
End Sub


Sub Macro2()
Dim ans As Variant

ans = MsgBox("Message", 1)
If ans = vbOK Then
Call [subroutine]
Else
Exit Sub
End If

End Sub
 
J

Jim Thomlinson

Extremely minor point (picky almost beyond reason) but you are better off to
declare ans as

Dim ans As VbMsgBoxResult

--
HTH...

Jim Thomlinson


Jacob Skaria said:
Try one of these

Sub Macro1()
If MsgBox("Message", 1) <> vbOK Then Exit Sub
Call subroutine
End Sub


Sub Macro2()
Dim ans As Variant

ans = MsgBox("Message", 1)
If ans = vbOK Then
Call [subroutine]
Else
Exit Sub
End If

End Sub

--
Jacob


Pete said:
I am attempting to use the Msgbox function with the Ok/Cancel buttons. the
code I have written thus far is below:

Msgbox("Message",1)

If ans = vbOk then call [subroutine]
If ans = vbCancel the exit sub

end if

end sub

The subroutine does not work when the "Ok" button is used. I have tried
other variations on this to no avail. Could anyone please assist?
 
D

Dave Peterson

I'd use:

Dim ans as long

If I recall correctly (and I may not <bg>), this type doesn't exist in earlier
versions of excel (xl2k added it????).

I remember posting a suggestion that used it and the OP had trouble--but that
was a pretty long time ago.

Jim said:
Extremely minor point (picky almost beyond reason) but you are better off to
declare ans as

Dim ans As VbMsgBoxResult

--
HTH...

Jim Thomlinson

Jacob Skaria said:
Try one of these

Sub Macro1()
If MsgBox("Message", 1) <> vbOK Then Exit Sub
Call subroutine
End Sub


Sub Macro2()
Dim ans As Variant

ans = MsgBox("Message", 1)
If ans = vbOK Then
Call [subroutine]
Else
Exit Sub
End If

End Sub

--
Jacob


Pete said:
I am attempting to use the Msgbox function with the Ok/Cancel buttons. the
code I have written thus far is below:

Msgbox("Message",1)

If ans = vbOk then call [subroutine]
If ans = vbCancel the exit sub

end if

end sub

The subroutine does not work when the "Ok" button is used. I have tried
other variations on this to no avail. Could anyone please assist?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top