Sunteți pe pagina 1din 9

Example 1: How To Access The Excel Range And Show The Value Using Message Box

Sub sbExample1()

'It will display the A5 value in the message Box


MsgBox Range("A5")

'You can also use Cell Object to refer A5 as shwon below:


MsgBox Cells(5, 1) 'Here 5 is Row number and 1 is Column number
End Sub

Example 2: How To Enter Data into a Cell

'Example 2: How To Enter Data into a Cell


Sub sbExample2()

'It will enter the data into B5


Range("B5") = "Hello World! using Range"

'You can also use Cell Object as shwon below B6:


Cells(6, 2) = "Hello World! using Cell" 'Here 6 is Row number and 2 is
Column number
End Sub

Example 3: How To Change The Background Color Of A Particular Range

'Example 3: How To Change The Background Color Of A Particular Range


Sub sbExample3()

'You can set the background color using Interior.ColorIndex Property


Range("B1:B5").Interior.ColorIndex = 5 ' 5=Blue

End Sub

Example 4: How To Change The Font Color And Font Size Of A Particular Range

'Example 4: How To Change The Font Color And Font Size Of A Particular Range
Sub sbExample4()

'You can set the background color using Interior.ColorIndex Property


Range("B1:B10").Font.ColorIndex = 3 ' 3=Red

End Sub

Example 5: How To Change The Text To Upper Case Or Lower Case

1
'Example 5: How To Change The Text To Upper Case Or Lower Case
Sub sbExample5()

'You can use UCase finction to change the text into Upper Case
Range("C2").Value = UCase(Range("C2").Value)

'You can use LCase finction to change the text into Upper Case
Range("C3").Value = LCase(Range("C3").Value)

End Sub

Example 6: How To Copy Data From One Range To Another Range

Example 6: How To Copy Data From One Range To Another Range


Sub sbExample6()

'You can use Copy method


Range("A1:D11").Copy Destination:=Range("H1")

End Sub

Example 7: How To Select And Activate Worksheet

'Example 7: How To Select And Activate Worksheet


Sub sbExample7()

'You can use Select Method to select


Sheet2.Select

'You can use Acctivate Method to activate


Sheet1.Activate

End Sub

Example 8: How To Get The Active Sheet Name And Workbook Name

'Example 8: How To Get The Active Sheet Name And Workbook Name
Sub sbExample8()

'You can use ActiveSheet.Name property to get the Active Sheet name
MsgBox ActiveSheet.Name

'You can use ActiveWorkbook.Name property to get the Active Workbook name
MsgBox ActiveWorkbook.Name

End Sub

Example 9: How To Add New Worksheet And Rename A Worksheet and Delete Worksheet

2
'Example 9: How To Add New Worksheet And Rename A Worksheet and Delete
Worksheet
Sub sbExample9()

'You can use Add method of a worksheet


Sheets.Add

'You can use Name property of worksheet


ActiveSheet.Name = "Temporary Sheet"

'You can use Delete method of a worksheet


Sheets("Temporary Sheet").Delete

End Sub

Example 10: How To Create New Workbook, Add Data, Save And Close The Workbook

'Example 10: How To Create New Workbook, Add Data, Save And Close The
Workbook
Sub sbExample10()

'You can use Add method of a Workbooks


Workbooks.Add

'You can use refer parent and child object to access the range
ActiveWorkbook.Sheets("Sheet1").Range("A1") = "Sample Data"

'It will save in the deafult folder, you can mention the full path as
"c:\Temp\MyNewWorkbook.xls"
ActiveWorkbook.SaveAs "MyNewWorkbook.xls"

ActiveWorkbook.Close

End Sub

Example 11: How To Hide And Unhide Rows And Columns

'Example 11: How To Hide And Unhide Rows And Columns


Sub sbExample11()

'You can use Hidden Propery of Rows


Rows("12:15").Hidden = True 'It will Hide the Rows 12 to 15

Rows("12:15").Hidden = False 'It will UnHide the Rows 12 to 15

'You can use Hidden Propery of Columns


Columns("E:G").Hidden = True 'It will Hide the Rows E to G

Columns("E:G").Hidden = False 'It will UnHide the Rows E to G

End Sub

3
Example 12: How To Insert And Delete Rows And Columns

'Example 12: How To Insert And Delete Rows And Columns


Sub sbExample12()

'You can use Insert and Delete Properties of Rows


Rows(6).Insert 'It will insert a row at 6 row

Rows(6).Delete 'it will delete the row 6

'You can use Insert and Delete Properties of Columns


Columns("E").Insert 'it will insert the column at E

Columns("E").Delete 'it will delete the column E

End Sub

Example 13: How To Set The Row Height And Column Width

'Example 13: How To Set The Row Height And Column Width
Sub sbExample13()

'You can use Hidden Propery of Rows


Rows(12).RowHeight = 33

Columns(5).ColumnWidth = 35

End Sub

Example 14: How To Merge and UnMerge Cells

'Example 14: How To Merge and UnMerge Cells


Sub sbExample14()

'You can use Merge Property of range


Range("E1:E5").Merge

'You can use UnMerge Property of range


Range("E1:E5").UnMerge

End Sub

Example 15:

How To Compare Two Values – A Simple Example On If Condition

4
VBA WITH MACRO SAMPLE CODE

How To Print 1000 Values – A Simple Example On For Loop

'Example 15:
'How To Compare Two Values – A Simple Example On If Condition
'How To Print 1000 Values – A Simple Example On For Loop
Sub sbExample15()

'A Simple Example On If Condition


'We will campare A2 and A3 value using If
If Range("A2").Value = Range("A3") Then
MsgBox "True"
Else
MsgBox "False"
End If

'A Simple Example On For Loop


'We will print 1- 1000 integers in the Column E

For i = 1 To 1000
Cells(i, 5) = i 'here 5= column number of E
Next i

'showing status once it is printed the 1000 numbers


MsgBox "Done! Printed 1000 integers in Column E"

End Sub

CONTROL STATEMENT SAMPLE CODE


Sub If_ElseIf_Else()

'Variable declaration
Dim Num As Integer

'Accepting the number by the user


Num = InputBox("Enter the Number:", "Number")

If Num < 0 Then


'Check whether the number is less than zero

5
MsgBox "Number is negative."
ElseIf Num = 0 Then
'Check whether the number is equal to zero
MsgBox "Number is zero."
Else
'Check whether the number is greater than zero
MsgBox "Number is positive."
End If

End Sub

Sub If_ElseIf_ElseIf()

'Variable declaration
Dim Num As Integer

'Accepting the number by the user


Num = InputBox("Enter the Number:", "Number")

If Num < 0 Then


'Check whether the number is less than zero
MsgBox "Number is negative."
ElseIf Num = 0 Then
'Check whether the number is equal to zero
MsgBox "Number is zero."
ElseIf Num > 0 Then
'Check whether the number is greater than zero
MsgBox "Number is positive."
End If

End Sub

Sub NestedIf_FindQuater()

'Variable declaration
Dim Mnt As String

'Accepting the month by the user


Mnt = InputBox("Enter the Month:", "Month")

If Mnt = "January" Or Mnt = "February" Or Mnt = "March" Then


'Check if the month is fall under first quater.
MsgBox "First Quater."
ElseIf Mnt = "April" Or Mnt = "May" Or Mnt = "June" Then
'Check if the month is fall under second quater.
MsgBox "Second Quater."
ElseIf Mnt = "July" Or Mnt = "August" Or Mnt = "September" Then
'Check if the month is fall under third quater.
MsgBox "Third Quater."
Else

6
'Check if the month is fall under fourth quater.
MsgBox "Fourth Quater."
End If

End Sub

Sub NestedIf_StudentGrade()

'Variable declaration
Dim Mrks As String

'Accepting the month by the user


Mrks = InputBox("Enter the Marks:", "Marks")

If Mrks <= 100 And Mrks >= 90 Then


'Check if the Grade A++
MsgBox "Grade : A++"
ElseIf Mrks < 90 And Mrks >= 80 Then
'Check if the Grade A+
MsgBox "Grade : A+"
ElseIf Mrks < 80 And Mrks >= 60 Then
'Check if the Grade A
MsgBox "Grade : A"
ElseIf Mrks < 60 And Mrks >= 50 Then
'Check if the Grade B
MsgBox "Grade : B"
ElseIf Mrks < 50 And Mrks >= 35 Then
'Check if the Grade C
MsgBox "Grade : C"
Else
'Check if the Grade has fail
MsgBox "Grade : Fail"
End If

End Sub

Sub sb3_SelectCaseCondition()
Select Case Range("B2")
Case "D1"
Range("D2") = Range("C2") * 10 / 100
Case "D2"
Range("D2") = Range("C2") * 20 / 100
Case "D3", "D4"
Range("D2") = Range("C2") * 15 / 100
Case Else
Range("D2") = Range("C2") * 5 / 100
End Select
End Sub

7
Sub sbForLoop()
Dim iCntr As Integer
For iCntr = 1 To 5
msgbox iCntr
Next
End Sub

Sub sb4_ForLoop()
Dim iCntr As Integer
For iCntr = 2 To 16
Cells(iCntr, 4) = Cells(iCntr, 3) * 10 / 100
Next
End Sub

Sub sbForLoop()
Dim iCntr As Integer
For iCntr = 1 To 10 Step 2
msgbox iCntr
Next
End Sub

Sub sbForEachLoop()
Dim Sht As Worksheet
For Each Sht In ThisWorkbook.Sheets
MsgBox Sht.Name
Next
End Sub

Dim i As Integer
i=InputBox (&quot;Please Enter a value to calculate square value:&quot;)
MsgBox &quot;Square Value of a given number is: &quot; &amp; i*i

Private Sub CommandButton1_Click()


Call FindSquare
End Sub
Sub FindSquare()

Dim i As Integer

8
i=InputBox (&quot;Please Enter a value to
calculate square value:&quot;)
MsgBox &quot;Square Value of a given number
is: &quot; &amp; i*i

End Sub

S-ar putea să vă placă și