![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Board Regular
Join Date: Feb 2002
Posts: 64
|
hi ive got a delete student macro that i need to build - the actual code is no problem - a simple For...Next and If combination. The problem is that i need a little confirmation dialog box to pop up with Yes and No buttons - and when i click Yes for it to run the code but if No then to display another message box saying its cancelled and then exit sub. ive been trying to do it all day, looking at books but excel wont let me write the msg box with the Yes/No buttons as it keeps asking for the msg box to be equal to something. How can i accomplish it all in one single macro rather than a few? Thanks very much in advance.
Daniel C. |
|
|
|
|
|
#2 |
|
Board Regular
Join Date: Feb 2002
Posts: 64
|
dyou know Dave, i expect you do
|
|
|
|
|
|
#3 |
|
MrExcel MVP
Join Date: Feb 2002
Location: San Francisco, California USA
Posts: 10,387
|
Try something like this:
Sub MessageBoxMania() Dim Resp As Integer Resp = MsgBox("Are you sure you want to proceed?", 36, "Please confirm") If Resp = 6 Then 'Yes was pressed, your macro goes here Else 'No was pressed, here's message tellingthem action is cancelled. MsgBox "Please click OK, no harm no foul.", 64, "Macro cancelled." End If End Sub |
|
|
|
|
|
#4 |
|
Board Regular
Join Date: Feb 2002
Posts: 64
|
cheers tom ! how come you use numbers 36 and 64 do they represent vbyes and vbno?
|
|
|
|
|
|
#5 |
|
MrExcel MVP
Join Date: Feb 2002
Location: San Francisco, California USA
Posts: 10,387
|
It's a habit I got into; those particular numbers represent icons on the message box, one for vbQuestion with a YesNo button combination, and the other for vbInformation, with just an OK button.
After I got used to those button integer values, it kinda stuck in my head as more logical and efficient to type a 2 digit number than all those boring code words. The 6 refers to vbYes. Just a personal preference for code writing. |
|
|
|
|
|
#6 |
|
Banned
Join Date: Feb 2002
Posts: 1,582
|
Hi Daniel
I see Tom has already supplied you answer, but the VbYes VbNo VbCancel VbOk etc etc all have values, as Tom has shown. I am more inclined to pasre VbYes or VbNo myself as it makes the code a bit easier to read. |
|
|
|
|
|
#7 |
|
Board Regular
Join Date: Feb 2002
Posts: 64
|
thanks to both of you... Ill get back to coding in a minute...
|
|
|
|
|
|
#8 |
|
Board Regular
Join Date: Feb 2002
Location: Ahmedabad Gujarat
Posts: 303
|
May below code work for you...
Private Sub workingmessagebox() Dim msg As String Dim response As Integer msg = "Was this file opened up through " & vbCrLf msg = msg & "Excel or was it double clicked on." response = MsgBox(msg, vbYesNoCancel + vbInformation) ' It is easy to work with select when there is several ' conditions to check. ' practicaly it gives same performance as if ...endif 'but the code with select statement is easy to read ' if you need more help in message box type "msgbox" ' in search box of agent you will given full help ' how combobox works. Select Case response Case Is = vbYes MsgBox ("You pressed yes button") Case Is = vbNo MsgBox ("you pressed no button") Case Is = vbCancel MsgBox ("You pressed cancel button") End Select 'I have written code for if....elseif...then..endif ' you can remove coment marks and try with this also 'If response = vbYes Then ' 'MsgBox ("You pressed yes button") 'ElseIf response = vbNo Then 'MsgBox ("You pressed No button") 'ElseIf response = vbCancel Then 'MsgBox ("You pressed Cancel button") ' 'End If ' hope you will enjoy End Sub nishith desai http://www.pexcel.com |
|
|
|
|
|
#9 |
|
Board Regular
Join Date: Feb 2002
Location: Tulsa, OK
Posts: 354
|
Cool!
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|