\nvbMsgBoxRtlReading<\/td>\n 1048576<\/td>\n This is for text to be displayed from Right to left. Arabic language<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\nAs you can see, All the parameters are categorized in 4 groups.[\/fusion_text][title size=”1″ content_align=”left” style_type=”none” sep_color=”” margin_top=”” margin_bottom=”” class=”” id=””]Rule to combine these parameters[\/title][fusion_text]It is a very simple rule. You can use only ONE parameters from each group at max. This means, in total, you can combine 4 parameters in a message box.<\/p>\n
Buttons like Ok, Cancel, Yes, No etc. should have conditional statements to execute. \nFor example, if a message box has a Prompt like “do you want to continue?” with Yes and No button on it. \nWe should be able to capture the action which use has taken and based on that you start execution of that particular section.<\/p>\n
Therefore, msgBox funciton returns some value on pressing the buttons. Even for cancel button, you should write code, if you want to cancel the program.<\/p>\n
Let’s have a look the values what are returned by pressing different buttons from message box.[\/fusion_text][title size=”1″ content_align=”left” style_type=”default” sep_color=”” margin_top=”” margin_bottom=”” class=”” id=””]Values returned by MsgBox Function on Pressing each Button[\/title][fusion_text]Since MsgBox<\/strong> works like a VBA function<\/strong> as well, each button of message box, returns a specific value when user presses it.<\/p>\nHere is the list of values returned by MsgBox function on pressing buttons[\/fusion_text][fusion_text]<\/p>\n
\n
\n\n\nButton Name<\/th>\n Returned Constant<\/th>\n Returned Numeric Value<\/th>\n<\/tr>\n<\/thead>\n \n\nOK Button<\/td>\n vbOK<\/td>\n 1<\/td>\n<\/tr>\n \nCancel Button<\/td>\n vbCancel<\/td>\n 2<\/td>\n<\/tr>\n \nAbort Button<\/td>\n vbAbort<\/td>\n 3<\/td>\n<\/tr>\n \nRetry Button<\/td>\n vbRetry<\/td>\n 4<\/td>\n<\/tr>\n \nIgnore Button<\/td>\n vbIgnore<\/td>\n 5<\/td>\n<\/tr>\n \nYes Button<\/td>\n vbYes<\/td>\n 6<\/td>\n<\/tr>\n \nNo Button<\/td>\n vbNo<\/td>\n 7<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n <\/p>\n<\/div>\n
[\/fusion_text][fusion_text]By capturing the returned value from MsgBox<\/strong> function, you can always execute Excel Macro conditionally. Example: If Pressed yes.. then do this.. If No then do this.<\/p>\nBelow are the examples where I have used MsgBox as a function to capture the response and do different tasks based on the buttons pressed.[\/fusion_text][title size=”1″ content_align=”left” style_type=”none” sep_color=”” margin_top=”” margin_bottom=”” class=”” id=””]Examples : Few most frequently used Message Box Types[\/title][fusion_text]Below examples will give you an idea, how you can capture the response from the message box and execute specific piece of codes. \nYou can use values from the above table and using these examples you can control the flow of execution of your macro, however you want.[\/fusion_text][title size=”1″ content_align=”left” style_type=”none” sep_color=”” margin_top=”20px” margin_bottom=”” class=”” id=””]Message box with Yes\/No and execute macro only if it is pressed Yes or No[\/title][fusion_text]<\/p>\n
\r\nSub msgBoxWithYesNoWhenYesOrNoPressed()\r\n Dim promptMessage\r\n Dim btnStyle\r\n Dim titleOfTheMessageBox\r\n Dim resMsgBox\r\n \r\n promptMessage = \"Are you sure you want to clear data from Entire Sheet?\"\r\n btnStyle = vbCritical + vbYesNo\r\n titleOfTheMessageBox = \"Decistion Box - Clear Data\"\r\n \r\n resMsgBox = MsgBox(Prompt:=promptMessage, Buttons:=btnStyle, Title:=titleOfTheMessageBox)\r\n If resMsgBox = vbYes Then ' or resMsgBox = 6 -- refer the above table\r\n Debug.Print \"User has pressed Yes... write all the statement which you want to be executed when pressess Yes\"\r\n Else\r\n Debug.Print \"User has pressed Yes... write all the statement which you want to be executed when pressess No\"\r\n End If\r\nEnd Sub\r\n\r\n<\/code><\/pre>\nIn the above example, since, there are two buttons only, Yes and No<\/em><\/strong>, therefore, it make sense to assume that, If user has not pressed Yes, then it would have pressed No. That is why I have put the code in Else condition.<\/p>\nbut if you have more than two buttons, then like vbYes (6)<\/strong>, you can also check the response with vbNo (7)<\/strong> and then execute the relevant macro.<\/p>\n\r\nSub msgBoxWithYesNoWhenNoPressed()\r\n Dim promptMessage\r\n Dim btnStyle\r\n Dim titleOfTheMessageBox\r\n Dim resMsgBox\r\n \r\n promptMessage = \"Are you sure you want to clear data from Entire Sheet?\"\r\n btnStyle = vbCritical + vbYesNo\r\n titleOfTheMessageBox = \"Decistion Box - Clear Data\"\r\n \r\n resMsgBox = MsgBox(Prompt:=promptMessage, Buttons:=btnStyle, Title:=titleOfTheMessageBox)\r\n If resMsgBox = vbYes Then ' or resMsgBox = 6 -- refer the above table\r\n Debug.Print \"User has pressed Yes... write all the statement which you want to be executed when pressess Yes\"\r\n Exit Sub\r\n End If\r\n \r\n If resMsgBox = vbNo Then ' or resMsgBox = 7 -- refer the above table\r\n Debug.Print \"User has pressed Yes... write all the statement which you want to be executed when pressess No\"\r\n End If\r\nEnd Sub\r\n<\/code><\/pre>\n[\/fusion_text][title size=”1″ content_align=”left” style_type=”none” sep_color=”” margin_top=”20px” margin_bottom=”” class=”” id=””]Execute specific macro when pressed Cancel button from Message Box[\/title][fusion_text]In the below example, you can see, I have created 3 if-else clause for Yes, No and Cancel button separately. <\/p>\n
You can execute different macros on pressing different buttons.<\/p>\n
\r\nSub msgBoxWithYesNoWhenCancelPressed()\r\n Dim promptMessage\r\n Dim btnStyle\r\n Dim titleOfTheMessageBox\r\n Dim resMsgBox\r\n \r\n promptMessage = \"Are you sure you want to clear data from Entire Sheet?\"\r\n btnStyle = vbCritical + vbYesNoCancel\r\n titleOfTheMessageBox = \"Decistion Box - Clear Data\"\r\n \r\n resMsgBox = MsgBox(Prompt:=promptMessage, Buttons:=btnStyle, Title:=titleOfTheMessageBox)\r\n If resMsgBox = vbYes Then ' or resMsgBox = 6 -- refer the above table\r\n Debug.Print \"User has pressed Yes... write all the statement which you want to be executed when pressess Yes\"\r\n Exit Sub\r\n End If\r\n \r\n If resMsgBox = vbNo Then ' or resMsgBox = 7 -- refer the above table\r\n Debug.Print \"User has pressed Yes... write all the statement which you want to be executed when pressess No\"\r\n Exit Sub\r\n End If\r\n \r\n If resMsgBox = vbCancel Then ' or resMsgBox = 2 -- refer the above table\r\n Exit Sub ' to exit from the program immediately\r\n End If\r\nEnd Sub\r\n<\/code><\/pre>\n[\/fusion_text][fusion_text]<\/p>\n
\n
Conclusion:<\/h2>\n Now you know enough about message box in Excel VBA. You can display really awesome message boxes with your own Title, Prompt and different buttons.<\/p>\n
If you have any question, doubt or suggestion, put them in the comment below. I would to respond as soon as possible.<\/p><\/div>\n
[\/fusion_text]<\/p>\n<\/span>","protected":false},"excerpt":{"rendered":"[fusion_text]ear friends, You must have seen windows message boxes. As the names suggests, they are basically used as a popup to give some message to the user on screen. Based on type of message you are sending, you can format your message box accordingly. With Type of Message, I mean, is it a warning? Is […]<\/p>\n","protected":false},"author":45,"featured_media":14372,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1246,1675],"tags":[],"class_list":["post-12058","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-macro","category-excel-macro-for-beginners"],"yoast_head":"\n
Excel VBA Tutorial -All you need to know about Message Box in Excel VBA<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n \n\t \n\t \n\t \n