How to get Inputs from User in VBA Script ? & How to give Message to User?
In this blog we will learn InputBox and MsgBox in VBA. Input Box use to get user inputs in between a VBA code execution. However MsgBox use to give Message to user while user Running a program.
1. VBA InputBox
The InputBox function provides the functionality of entering values by user. When user Run the VBA Program user got a input Box opened between program. Now user require to enter input as asked by programmer and press Ok Button. Once user pass his input and press on ok button program start executing next coding steps. Refer Below image to under stand the process.
In above image we can see user input Box , where user require to enter input number and press on ok button. Now take a look on below image where user input is 1. Now user needs to press on ok button.
Let see what happened when user entered 1 number and press on ok button. we got entered 1 number in Range("A1"). But Why ? Take a look on VBA Code which we wrote in
Visual Basic Editor. Here in Code we mentioned IF condition code and we wrote (Range("A1").Value = i) on if condition is True. However on False condition we wrote (Range("A1").Value = "Exit from code").
Let see When user enter > 1 Number in Input Box. As per written code in Visual basic editor. False condition will work and it will entered (Exit from code) text in Range("A1").
1. VBA Message Box (Msg Box)
MsgBox is a dialog box which is use to show messages to our users as per our requirements. Here we are taking basic examples of MsgBox. we are using Input Box and Msgbox in below example. Take a look on below image.
When we will run above code . It will ask to enter input to user as we already explained in Input Box. As per user input we passed two messages for user. Let see below images to understand the execution process of VBA Code.
When user enter 1 number in input box If condition's logical test result will be True , So that Msg Box result will print (User wants to continue)as below image.
Let see what happened if user enter > 1 number (example user enter 5 number) in input Box.
As per written code in Visual basic editor. False condition will work and it will give (
Exit from code) text in Msg Box as a program output.
No comments:
Post a Comment
Feel free to ask your questions regarding Ms Excel & VBA macros.