VBA Unit III
VBA Unit III
Creating Sub and Function procedures, calling them, and passing arguments to them are
fundamental skills in VBA (Visual Basic for Applications) programming. These techniques help
you organize your code, make it more reusable, and improve its readability. Let's dive into how
to create, call, and pass arguments to Sub and Function procedures in VBA.
A Sub procedure is a block of code that performs actions but does not return a value.
Syntax
Sub ProcedureName([arguments])
' Code to execute
End Sub
ProcedureName: The name of the Sub procedure.
arguments (optional): Parameters you want to pass to the procedure.
Syntax
The MsgBox function in VBA is used to display messages to the user. It can also be used to
gather simple user responses (like Yes/No or OK/Cancel).
Syntax
Sub ShowMessage()
MsgBox "Hello, User!"
End Sub
Gathering User Information: Input Boxes
The InputBox function in VBA is used to prompt the user to enter information. The value
entered by the user is returned as a string.
Syntax
Sub GetUserName()
Dim userName As String
userName = InputBox("Please enter your name:", "User Input")
If userName <> "" Then
MsgBox "Hello, " & userName & "!", vbInformation, "Greeting"
Else
MsgBox "You did not enter your name.", vbExclamation, "Warning"
End If
End Sub
Command buttons are used to execute code when clicked by the user.
2. List Boxes
List boxes display a list of items from which the user can select one or more.
Example
3. Combo Boxes
Combo boxes combine a text box with a list box, allowing the user to select from the list or enter
their own value.
Example
Private Sub UserForm_Initialize()
ComboBox1.AddItem "Option 1"
ComboBox1.AddItem "Option 2"
ComboBox1.AddItem "Option 3"
End Sub
4. Labels
Example
5. Option Buttons
Option buttons (also known as radio buttons) allow the user to select one option from a group of
options.
Example
6. Check Boxes
Example
7. Text Boxes
8. Frames
Frames are used to group related controls together, making the form more organized.
Example
The Font property is used to format the text within a cell or a range of cells. Here are some
common attributes you can set using the Font property:
Bold
Italic
Name (Font Name)
Size (Font Size)
Color
Example: Setting Font Properties
Sub FormatFont()
With Range("A1")
.Value = "Hello, World!"
.Font.Name = "Arial"
.Font.Size = 14
.Font.Bold = True
.Font.Italic = True
.Font.Color = RGB(255, 0, 0) ' Red color
End With
End Sub
In this example, the text in cell A1 is set to Arial, size 14, bold, italic, and colored red.
The Interior property is used to format the background of a cell or a range of cells. Common
attributes you can set using the Interior property include:
Color
Pattern
PatternColor
PatternColorIndex
Example: Setting Background Color
Sub FormatBackground()
With Range("B2")
.Value = "Formatted Cell"
.Interior.Color = RGB(200, 200, 255) ' Light blue background
End With
End Sub
Creating a Counter
A counter is a variable that keeps track of the number of occurrences or the amount of
something. In VBA, you can create a counter using variables and loops.
Let's create a simple counter that counts from 1 to 10 and displays the count in a message box.
Sub SimpleCounter()
Dim i As Integer
For i = 1 To 10
MsgBox "Count: " & i
Next i
End Sub
String Handling
VBA provides several functions for handling strings, including concatenation, finding substrings,
and manipulating string contents.
1. Concatenation
Sub ConcatenateStrings()
Dim str1 As String
Dim str2 As String
Dim result As String
2. Finding Substrings
You can find substrings within a string using the InStr function.
Sub FindSubstring()
Dim mainString As String
Dim subString As String
Dim position As Integer
3. Extracting Substrings
Sub ExtractSubstring()
Dim mainString As String
Dim subString As String
4. Changing Case
You can change the case of strings using the UCase, LCase, and StrConv function
Sub ChangeCase()
Dim originalString As String
Dim upperCaseString As String
Dim lowerCaseString As String
Dim properCaseString As String
upperCaseString = UCase(originalString)
lowerCaseString = LCase(originalString)
properCaseString = StrConv(originalString, vbProperCase)
5. Trimming Spaces
You can trim leading and trailing spaces from a string using the Trim function.
Sub TrimSpaces()
Dim originalString As String
Dim trimmedString As String
MsgBox "Original: '" & originalString & "'" & vbCrLf & _
"Trimmed: '" & trimmedString & "'"
End Sub
6. String Length
You can find the length of a string using the Len function.
Sub StringLength()
Dim myString As String
Dim length As Integer
In VBA (Visual Basic for Applications), the Left and Right string functions are used to extract
a specified number of characters from the beginning or end of a string, respectively. These
functions are useful for parsing and manipulating text data.
Left Function
The Left function returns a specified number of characters from the beginning (left side) of a
string.
Syntax
Left(string, length)
string: The original string from which you want to extract characters.
length: The number of characters to extract from the left side of the string.
Right Function
The Right function returns a specified number of characters from the end (right side) of a string.
Syntax
string: The original string from which you want to extract characters.
length: The number of characters to extract from the right side of the string.
Sub ExampleRightFunction()
Dim originalString As String
Dim rightString As String