Documente Academic
Documente Profesional
Documente Cultură
Sub sbExample1()
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()
End Sub
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
End Sub
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()
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 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
End Sub
3
Example 12: How To Insert And Delete Rows And Columns
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()
Columns(5).ColumnWidth = 35
End Sub
End Sub
Example 15:
4
VBA WITH MACRO SAMPLE CODE
'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()
For i = 1 To 1000
Cells(i, 5) = i 'here 5= column number of E
Next i
End Sub
'Variable declaration
Dim Num As Integer
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
End Sub
Sub NestedIf_FindQuater()
'Variable declaration
Dim Mnt As String
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
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 ("Please Enter a value to calculate square value:")
MsgBox "Square Value of a given number is: " & i*i
Dim i As Integer
8
i=InputBox ("Please Enter a value to
calculate square value:")
MsgBox "Square Value of a given number
is: " & i*i
End Sub