Comments are used in the programming languages to mention or display useful information and this is not new to us if you have studied any programming language then you must have heard about it basically if we are writing any code then at some line of code we can add comments and explain the line of code so that if we handover our code to any other programmer he or she can understand it easily.

You can include information such as developed by, modified by, and can also include incorporated logic which is a sign of good programmers.

Comments written at any line of the code is not considered as a code i.e it is ignored by the interpreter while interpreting code written.

Use of Comments :- 

  • It is added with the purpose of making the code easier for humans to understand. 
  • Putting comments in your program helps you peers to understand the implemented logic easily.
  • It also helps in debugging errors very easily as comments are non executable statements. If an error is detected, we can simply comment that portion of code and debug it.

Comments in VBA can be denoted by two methods :

1. Statements that starts with a single Quote ( ' ) is treated as comment. 

For example -.
   
' Welcome to the comments with single quote.
' Coded by : Digitaliseit 
' Project : Example of Comments 


2. Statements that starts with a Keyword "REM".

For example - 

REM This is a comment using REM keyword
REM Explained by : Digitaliseit


Message Box :




The Message Box  function in VBA is used to display message box on the screen with the output whenever the user clicks a button the action is performed based on the button clicked by user.

Syntax :

MsgBox(prompt[,buttons][,title][,helpfile,context])

All these things that are inside this MsgBox() function are parameters or values that can be taken by MsgBox() function. Every parameter has different actions so let's discuss each one of it one by one.

MsgBox the value of Variable One is  & Variable_One & _

Chr(13) & the value of Variable Two is  & Variable_Two

If we want to display more than one variable value to the MsgBox then we can simply do it using the above script.

Parameter Description

Prompt - A Required Parameter. In this parameter whatever we will write will be displayed as a message in our MsgBox Dialog box. The maximum length of prompt is 1024 characters. If the message extends to more than one line , then lines can be separated using a carriage return character (Chr(13)) or a linefeed character (chr(10)) between each line.
 
Buttons - An Optional Parameter. A Numeric expression that specifies the type of buttons to display, the icon style to use, the identity of the default button, and the modality of the message box. If left blank, the default value for buttons is 0.

Title - An Optional Parameter. A String expression displayed in the title bar of the dialog box. If the title is left blank, the application name is placed in the title bar.

Helpfile - An Optional Parameter. A String expression that identifies the Help file to use for providing context-sensitive help for the dialog box.

Context -  An Optional Parameter. A Numeric expression that identifies the Help context number assigned by the Help author to the appropriate Help topic. If context is provided, helpfile must also be provided.


The Buttons parameter can take any of the following values either as a NUMBER or  ACTION 

or   vbOKOnly - Displays OK button only.

or   vbOKCancel - Displays OK and Cancel buttons.

2  or   vbAbortRetryIgnore - Displays Abort, Retry, and Ignore buttons.

3  or   vbYesNoCancel - Displays Yes, No, and Cancel buttons.

4  or   vbYesNo - Displays Yes and No buttons.

5  or   vbRetryCancel - Displays Retry and Cancel buttons.

16 or  vbCritical - Displays Critical Message icon.

32 or  vbQuestion - Displays Warning Query icon.

48 or  vbExclamation - Displays Warning Message icon.

64 or  vbInformation - Displays Information Message icon.

0   or  vbDefaultButton1 - First button is default.

256 or vbDefaultButton2 - Second button is default.

512 or  vbDefaultButton3 - Third button is default.

768 or  vbDefaultButton4 - Fourth button is default.

0  or  vbApplicationModal Application modal - The current application will not work until the user responds to the message box.

4096  or  vbSystemModal System modal - All applications will not work until the user responds to the message box.

The above values are logically divided into four groups: 

The First group (0 to 5) indicates the buttons to be displayed in the message box. 
The Second group (16, 32, 48, 64) describes the style of the icon to be displayed.
The Third group (0, 256, 512, 768) indicates which button must be the default. 
The Fourth group (0, 4096) determines the modality of the message box.



Input Box :




The InputBox function prompts the users to enter values. After entering the values, if the user clicks the OK button or presses ENTER on the keyboard, the InputBox function will return the text in the text box. If the user clicks the Cancel button, the function will return an empty string ("").

Syntax

InputBox(prompt[,title][,default][,xpos][,ypos][,helpfile,context])


Parameter Description

Prompt − A required parameter. A String that is displayed as a message in the dialog box. The maximum length of prompt is approximately 1024 characters. If the message extends to more than a line, then the lines can be separated using a carriage return character (Chr(13)) or a linefeed character (Chr(10)) between each line.

Title − An optional parameter. A String expression displayed in the title bar of the dialog box. If the title is left blank, the application name is placed in the title bar.

Default − An optional parameter. A default text in the text box that the user would like to be displayed.

XPos − An optional parameter. The position of X axis represents the prompt distance from the left side of the screen horizontally. If left blank, the input box is horizontally centered.

YPos − An optional parameter. The position of Y axis represents the prompt distance from the left side of the screen vertically. If left blank, the input box is vertically centered.

Helpfile − An optional parameter. A String expression that identifies the helpfile to be used to provide context-sensitive Help for the dialog box.

context − An optional parameter. A Numeric expression that identifies the Help context number assigned by the Help author to the appropriate Help topic. If context is provided, helpfile must also be provided.

Example-

Let us calculate the Volume of a Cuboid by getting values from the user at run time with the help of three input boxes (one for length, one for breadth and one for height).


Function Volume_cuboid() 
   Dim length As Double 
   Dim breadth As Double
   Dim height As Double
   
   length  = InputBox("Enter length ", "Enter a Number") 
   breadth = InputBox("Enter breadth", "Enter a Number") 
   height  = InputBox("Enter height", "Enter a Number") 
Volume_cuboid = length * breadth * height 
End Function




                                                                            


Post a Comment

Previous Post Next Post