VBA Course
Sub Write_a_macro()
MsgBox "My first macro"
MsgBox 1
'Cell refrencing- how to write any value in the cell on the sheet
'Method-1- Writing value into active cell
[Link] = " Good VBA"
[Link] = 1234
'Method-2- Writing value into non-active cell
'[Celladdress].Value = 1234
[a4].Value = 1234
'Method-3- Writing value for a range of cell
'[Cellarange].Value = 1234
[f6:f22].Value = "hassan"
'Method-3- Writing value for a cell
Range("B1").Value = "Pakistan"
'Method-3- Writing value for a range of cell
Range("B2:B10").Value = "Islamabad"
End Sub
Sub Copy_paste
Range("i1:i9") = "Capital"
Range("i1:i9").Copy
Range("j1:j9").PasteSpecial
[Link] = False
End Sub
Sub Font
Range("l1:l9") = "Capital"
Range("l1:l9").[Link] = "Poppins"
Range("l1:l9").[Link] = True
Range("l1:l9").[Link] = True
Range("l1:l9").[Link] = True
Range("l1:l9").[Link] = True
End Sub
Sub With_Block()
Range("m1:m9") = "Capital"
With Range("m1:m9").Font
.Name = "Poppins"
.Bold = True
.Italic = True
.Underline = True
.Strikethrough = True
End With
End Sub
Adding borders
Sub Borders()
Range("r1:r10") = "Great"
Range("r1:r10").[Link] = 3
Range("r1:r10").[Link] = vbRed
Range("r1:r10").[Link] = xlDouble
Range("r1:r10").[Link] = xldashed
Range("r1:r10").[Link] = xlContinuous
End Sub
Sub Alignment()
Range("t1:t10") = "VBA"
'Horizontal Alignment
Range("t1:t10").HorizontalAlignment = xlLeft
Range("t1:t10").HorizontalAlignment = xlRight
Range("t1:t10").HorizontalAlignment = xlCenter
'Vertical Alignment
Range("t1:t10").VerticalAlignment = xlTop
Range("t1:t10").VerticalAlignment = xlBottom
Range("t1:t10").VerticalAlignment = xlCenter
End Sub
Sub Font_Color()
Range("t1:t10") = "VBA"
'Method-1
'For the 8 standard colors
Range("t1:t10").[Link] = vbGreen
Range("t1:t10").[Link] = vbYellow
Range("t1:t10").[Link] = vbRed
'Method-2
'Through color-index
'color-index is till 56
Range("t1:t10").[Link] = 3
Range("t1:t10").[Link] = 4
Range("t1:t10").[Link] = 7
Range("t1:t10").[Link] = 43
End Sub
Sub Font_Color()
' Similar to the Font color
Range("t1:t10") = "VBA"
'Method-1
'For the 8 standard colors
Range("t1:t10").[Link] = vbGreen
'Method-2
'Through color-index
'color-index is till 56
Range("t1:t10").[Link] = 3
End Sub
Sub Pastespecial()
Range("v1:v10").Value = "Excel VBA Complete Cou"
Range("v1:v10").Copy
Range("w1:w10").Pastespecial xlPasteAllUsingSourceTheme
Range("w1:w10").Pastespecial xlPasteColumnWidths
[Link] = False
End Sub
Sub Orientation()
'To change the angle of text in a cell
Range("z1").Value = "Excel"
Range("z1").Orientation = 90
End Sub
Sub Wraptext()
Range("o1:o3").Value = "Excel- To change the angle of text in a cell"
Range("o1:o3").Wraptext = True
Range("o1:o3").Wraptext = False
End Sub
Sub Merge_Unmerge()
Use range of cells for unmerge
Range("o1").Value = "Excel- To change the angle of text in a cell"
Range("o1").Merge
Range("o1").UnMerge
End Sub
Sub Clear Cells()
Range("n1:q1").Value = "Excel- To change the angle of text in a cell"
Range("n1:q1").ClearFormats
Range("n1:q1").ClearContent
Range("n1:q1").ClearHyperlinks
Range("n1:q1").Clear
The last one “clear” clears everything
End Sub
Sub Delete Cells()
Range("n1:q1").Value = "Excel"
Range("n1:q1").Delete
Range("n1:q1").[Link]
Range("y1:y10").Value = "VBA"
Range("y1:y10").Delete
Range("y1:y10").[Link]
End Sub
Sub Insert_Row_Column()
Range("v:v").Insert
Range("2:2").Insert
Range("v1").[Link]
Range("v1").[Link]
End Sub
Sub Delete_Row_Column()
Range("r2").Delete
Range("v2").[Link]
Range("v3").Delete
Range("v3").[Link]
Range("v4:v10").[Link]
Range("v4:v12").[Link]
End Sub
Sub Column_Width()
Range("r2").ColumnWidth = 12
“Gives same result but two methods
Range("r2").[Link] = 12
Range("l2").[Link]
End Sub
Sub Row_Height()
Range("r2:r10").Rowheight = 12
‘Gives same result but two methods
Range("r2:r10").[Link] = 12
Range("l2:l10").[Link]
End Sub
Sub Select_Active()
Range("a1").Select
Range("a1:a10").Select
Range("a1").Activate
Range("a1:a10").Activate
End Sub
Sub Hide_Unhide_Columns()
Range("a:a").[Link] = True
Range("a:a").[Link] = False
Range("a:f").[Link] = True
Range("a:f").[Link] = False
End Sub
Sub Hide_Unhide_Rows ()
Range("1:1").[Link] = True
Range("1:1").[Link] = False
Range("1:10").[Link] = True
Range("1:10").[Link] = False
End Sub
Sub Sheet_Refrencing()
‘Used to refer an inactivated sheet
Sheets("Jeeto Pakistan").Range("a1:a10").Value = "Kamran"
End Sub
Sub Add_Sheets()
'Method-1, to add sheets
[Link]
[Link]
'To add sheets at specific place
[Link] After:=Sheets("Jeeto Pakistan")
[Link] Before:=Sheets("Jeeto Pakistan")
End Sub
Sub Add_Sheets_Name()
‘Sheet will be added before the active cell
[Link] = "Fahad Khan"
End Sub
Sub Rename_Sheets()
‘Method-1
Sheets(1).Name = "Host"
‘(1) refers to sheet number
‘Method-2
Sheets("Fadi").Name = "zadiq"
End Sub
Sub Get_SheetNames()
'To get sheet names in a messagebox
MsgBox Sheets(1).Name
MsgBox Sheets(2).Name
MsgBox Sheets(3).Name
MsgBox Sheets(4).Name
MsgBox Sheets(5).Name
End Sub
Sub Copy_Paste_Sheets()
Sheets("zadiq").Copy after:=Sheets("Jeeto Pakistan")
Sheets("zadiq").Copy before:=Sheets("Jeeto Pakistan")
End Sub
Sub Move_Sheets()
Sheets("zadiq").Move after:=Sheets("Jeeto Pakistan")
Sheets("zadiq").Move before:=Sheets("Jeeto Pakistan")
End Sub
Sub Change_Sheets_TabColor()
Sheets("zadiq").[Link] = vbBlue
Sheets("Jeeto Pakistan").[Link] = 8
Sheets("Fahad Khan").[Link] = vbRed
Sheets("zadiq").[Link] = False
End Sub
Sub Hide_Unhide_Sheets()
Sheets("Sheet1").Visible = False
Sheets("Sheet1").Visible = True
End Sub
Sub Protect_Unprotect_Sheets()
When sheet is protected using this method the password does not work to unprotect the sheet.
Manual sheet protection is better.
Sheets("Sheet1").Protect Password = 123
Sheets("Sheet1").Unprotect Password = 123
End Sub
Sub Select_Active_Sheets()
Sheets("Learning VBA").Select
Sheets("Learning VBA").Activate
End Sub
Sub Add_Workbook()
[Link]
[Link] Filename:="E:/[Link]"
‘In the filename, give proper folder address.
End Sub
Sub Get_Workbook_Names()
'To get sheet names in a message box
MsgBox ([Link])
MsgBox ([Link])
MsgBox ([Link])
Sub Workbook_Save_Close()
Workbooks("[Link]").Sheets(4).Range("b1:b10") = "Geo Pakistan"
Workbooks("[Link]").Save
Workbooks("[Link]").Close
End Sub
Sub Workbook_Open_Close()
[Link] Filename:="FORECASTING COURSE:/[Link]"
Workbooks("[Link]").Sheets(6).Range("m10") = "Good Job"
Workbooks("[Link] ").Save
Workbooks("[Link] ").Close
End Sub
Sub Workbook_Delete ()
Kill (“File location:/[Link]”)
Kill (“E:/Book1,xlsx”)
End sub
Sub Create_Folder()
MkDir ("C:/Pakistan")
End Sub
Sub Variable_Usage()
Var1 = "BBC News"
Range("d1:d10").Value = Var1
Range("e1:e10").Value = Var1
Range("f1:f10").Value = Var1
End Sub
Sub For_Loop()
Rem For Loop is used to repeat a function for given number of times.
Dim x as Integer
For x = 1 To 5
MsgBox "Hassan"
Next
End Sub
Sub For_Loop()
Rem For Loop is used to repeat a function for given number of times.
Dim x as Integer
For x = 1 To 5
MsgBox x
'Now the messagebox will show values of x
Next
End Sub
Sub For_Loop()
Rem For Loop is used to repeat a function for given number of times.
Dim x as Integer
For x = 1 To 5 step 2
MsgBox x
'Now the message box will show values of x+2
Next
End Sub
Sub For_Loop2()
Rem For Loop is used to repeat a function for given number of times.
Dim x As Integer
For x = 1 To 10000
Cells(x, 8).Value = x
'Now the message box will show values of x
Next
End Sub
Sub For_Loop2()
Rem For Loop is used to repeat a function for given number of times.
Dim x As Integer
For x = 1 To 10000 step 5
Cells(x, 9).Value = x
'Now the message box will show values of x after every 5 cells
Next
End Sub
Sub For_Loop3()
‘Get all 56 colors
Dim x As Integer
For x = 1 To 56
Cells(x, 12).Value = x
Cells(x, 13).[Link] = x
Next
End Sub
Sub For_Loop4()
‘To go in the reverse order
Dim x As Integer
For x = 56 To 1
Cells(x, 12).Value = x
Next
End Sub
Sub For_Loop4()
'To add values diagonally
Dim x As Integer
For x = 1 To 20
Cells(x, x).Value = x
Next x
End Sub
For Each Next Loop used for collecting objects of similar types.
Sub For_Each_Next1()
'To get names of sheets in a workbook
Dim Y As Worksheet
For Each Y In [Link]
MsgBox [Link]
Next
End Sub
'To do something repeatedly
Sub Loop_Do_While()
'To do something repeatedly
'To add a number to a value in one cell and write the answer in other cell
Dim a As Integr
a=1
Do While Cells(a, 1) <> ""
Cells(a, 2).Value = Cells(a, 2).Value + 10
a=a+1
Loop
End Sub