VBA Course
VBA Course
Sub Write_a_macro()
'Cell refrencing- how to write any value in the cell on the sheet
'Method-1- Writing value into active cell
ActiveCell.Value = " Good VBA"
ActiveCell.Value = 1234
'Method-2- Writing value into non-active cell
'[Celladdress].Value = 1234
[a4].Value = 1234
Sub Copy_paste
Range("i1:i9") = "Capital"
Range("i1:i9").Copy
Range("j1:j9").PasteSpecial
Application.CutCopyMode = False
End Sub
Sub Font
Range("l1:l9") = "Capital"
Range("l1:l9").Font.Name = "Poppins"
Range("l1:l9").Font.Bold = True
Range("l1:l9").Font.Italic = True
Range("l1:l9").Font.Underline = True
Range("l1:l9").Font.Strikethrough = 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").Borders.Weight = 3
Range("r1:r10").Borders.Color = vbRed
Range("r1:r10").Borders.LineStyle = xlDouble
Range("r1:r10").Borders.LineStyle = xldashed
Range("r1:r10").Borders.LineStyle = 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").Font.Color = vbGreen
Range("t1:t10").Font.Color = vbYellow
Range("t1:t10").Font.Color = vbRed
'Method-2
'Through color-index
'color-index is till 56
Range("t1:t10").Font.ColorIndex = 3
Range("t1:t10").Font.ColorIndex = 4
Range("t1:t10").Font.ColorIndex = 7
Range("t1:t10").Font.ColorIndex = 43
End Sub
Sub Font_Color()
Range("t1:t10") = "VBA"
'Method-1
'For the 8 standard colors
Range("t1:t10").Interior.Color = vbGreen
'Method-2
'Through color-index
'color-index is till 56
Range("t1:t10").Interior.ColorIndex = 3
End Sub
Sub Pastespecial()
End Sub
Sub Orientation()
Sub Wraptext()
Sub Merge_Unmerge()
Use range of cells for unmerge
End Sub
Range("n1:q1").Value = "Excel"
Range("n1:q1").Delete
Range("n1:q1").EntireRow.Delete
Range("y1:y10").Value = "VBA"
Range("y1:y10").Delete
Range("y1:y10").EntireColumn.Delete
End Sub
Sub Insert_Row_Column()
Range("v:v").Insert
Range("2:2").Insert
Range("v1").EntireColumn.Insert
Range("v1").EntireRow.Insert
End Sub
Sub Delete_Row_Column()
Range("r2").Delete
Range("v2").EntireRow.Delete
Range("v3").Delete
Range("v3").EntireColumn.Delete
Range("v4:v10").EntireColumn.Delete
Range("v4:v12").EntireRow.Delete
End Sub
Sub Column_Width()
Range("r2").ColumnWidth = 12
“Gives same result but two methods
Range("r2").Columns.ColumnWidth = 12
Range("l2").Columns.AutoFit
End Sub
Sub Row_Height()
Range("r2:r10").Rowheight = 12
‘Gives same result but two methods
Range("r2:r10").Rows.Rowheight = 12
Range("l2:l10").Rows.AutoFi
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").Columns.Hidden = True
Range("a:a").Columns.Hidden = False
Range("a:f").Columns.Hidden = True
Range("a:f").Columns.Hidden = False
End Sub
Sub Hide_Unhide_Rows ()
Range("1:1").Rows.Hidden = True
Range("1:1").Rows.Hidden = False
Range("1:10").Rows.Hidden = True
Range("1:10").Rows.Hidden = 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
Sheets.Add
Worksheets.Add
'To add sheets at specific place
Sheets.Add After:=Sheets("Jeeto Pakistan")
Sheets.Add Before:=Sheets("Jeeto Pakistan")
End Sub
Sub Add_Sheets_Name()
‘Sheet will be added before the active cell
Sheets.Add.Name = "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()
Sub Move_Sheets()
Sub Change_Sheets_TabColor()
Sheets("zadiq").Tab.Color = vbBlue
Sheets("Jeeto Pakistan").Tab.Color = 8
Sheets("Fahad Khan").Tab.Color = vbRed
Sheets("zadiq").Tab.Color = 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()
Workbooks.Add
Workbooks.Add.SaveAs Filename:="E:/Demobook.xlsx"
‘In the filename, give proper folder address.
End Sub
Sub Get_Workbook_Names()
'To get sheet names in a message box
MsgBox (Activeworkbook.Name)
MsgBox (Thisworkbook.Name)
MsgBox (Activeworkbook.Name)
Sub Workbook_Save_Close()
Workbooks("Book1.xlsm").Save
Workbooks("Book1.xlsm").Close
End Sub
Sub Workbook_Open_Close()
End Sub
Sub Workbook_Delete ()
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).Interior.ColorIndex = 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 ThisWorkbook.Sheets
MsgBox Y.Name
Next
End Sub