51 Ready To Use Macros V2
51 Ready To Use Macros V2
Hi,
I am Ajay Anand, a Civil Engineer by education, Highway Engineer by
profession and a Coder by interest.
I started XL n CAD in 2018, as an effort to learn and share Tips,
Shortcuts and Tutorials related to MS Excel & AutoCAD.
Considering my contributions towards the Excel Community, I was
presented with the Microsoft MVP award in April 2020.
This ebook contains 51 ready to use Macros, that will help you to automize your works in
Microsoft Excel. Detailed explanation of these codes is available in xlncad.com and my
YouTube Channel for Excel Tutorials.
Table of contents
VBA stands for Visual Basic for Applications. It is a custom version of the Visual
Basic programming language that has powered Microsoft Excel's macros since the
mid-1990s. The programming (VBA) interface of MS Excel will let you,
1. Create and Execute Macros (explained in the next section). Anything that the
user can do in Excel from the user interface can be done by writing code in
VBA for Excel.
2. Create User Defined Functions (UDFs).
3. Integrate MS Excel with other applications such as Microsoft Word,
PowerPoint, Outlook, Notepad, AutoCAD etc.
What is a Macro?
Macros are codes written in VBA (Visual Basic for Applications) or recorded in MS Excel
(or other applications) which helps to reduce the manual effort required for a job.
Macros save your time and headaches by automating repetitive tasks. They reduce
the possibility of human error that increases with many, repetitive keystrokes and
tasks. The most important features of an Excel macro is that it can be saved for future
use and can be shared among multiple users.
In layman's language, a macro is a recording of your routine steps in Excel that you can
replay using a single button
The best part is, you don’t need to be a programmer or know VBA to use Macros. You
just need to copy the codes shared here into the VBA Editor or Excel and hit the run
button to execute them. After using the program, save the file as a Macro enabled
workbook for future use.
5. Close the editor and Click on Macros in Developer Tab. You will get a dialogue box
with the list of Macros, ready for execution. Select a Macro, Click on Run button
to execute the macro.
Sub AddNewSheet()
Sheets.Add
End Sub
Sub AddMultipleSheets()
Call Sheets.Add(, , 2)
End Sub
Sub AddSheetsUserInput()
Dim i, j As Integer
i = Val(InputBox(“Enter the number of sheets to be added”))
For j = 1 To i
Worksheets.Add
Next j
End Sub
Sub AddAndNameWorksheets()
Dim i, j As Integer
i = Val(InputBox(“Enter the number of sheets to be added”))
For j = 1 To i
Worksheets.Add.Name = “XL n CAD ” & j
Next j
End Sub
Sub CreateSheetsUsingNames()
Range(“A1”).Select
Do Until ActiveCell.Value = “”
Sheets.Add.Name = ActiveCell.Value
Sheets(“Names”).Select
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Sub DeleteSheet()
Sheets("Sheet1").Delete
End Sub
Sub DeleteSheetUserInput()
Dim x As String
x = InputBox("Enter the name of the sheet to be deleted")
Sheets(x).Delete
End Sub
8. Insert a row (one row each) in between each row containing data
Sub InsertRows()
Range("A2").Select
Do Until ActiveCell.Value = ""
Selection.EntireRow.Insert
ActiveCell.Offset(2, 0).Select
Loop
End Sub
Sub Insert2Rows()
Range("A2").Select
Do Until ActiveCell.Value = ""
Selection.EntireRow.Insert
Selection.EntireRow.Insert
ActiveCell.Offset(3, 0).Select
Loop
End Sub
10. Insert Rows in between each row containing data, based on user
Input
Sub InsertNRows()
Range("A2").Select
Dim d, e As Integer
e = Val(InputBox("Enter the number of Rows to be inserted"))
Do Until ActiveCell.Value = “”
For d = 1 To e
Selection.EntireRow.Insert
Next d
ActiveCell.Offset(e + 1, 0).Select
Loop
End Sub
Sub DeleteRows()
Range("A2").Select
Do Until ActiveCell.Offset(1, 0).Value = ""
Selection.EntireRow.Delete
ActiveCell.Offset(1, 0).Select
Loop
End Sub
12. Delete blank Rows (2 rows each) in between rows containing data
Sub Delete2Rows()
Range("A2").Select
Do Until ActiveCell.Offset(2, 0).Value = ""
Selection.EntireRow.Delete
Selection.EntireRow.Delete
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Sub InsertColumns()
Range("B1").Select
Do Until ActiveCell.Value = ""
Selection.EntireColumn.Insert
ActiveCell.Offset(0, 2).Select
Loop
End Sub
Sub DeleteColumns()
Range("B1").Select
Do Until ActiveCell.Offset(0, 1).Value = ""
Selection.EntireColumn.Delete
ActiveCell.Offset(0, 1).Select
Loop
End Sub
Sub SimpleMessageBox()
MsgBox "Hi, Greetings from xlncad.com"
End Sub
Sub MessageBoxTitle()
MsgBox "Hi, Greetings from xlncad.com", , "XL n CAD"
End Sub
Sub DisplayValueFromSheet()
MsgBox "The value of cell A1 is " & Range("A1").Value
End Sub
Sub DisplayTextFromSheet()
MsgBox "The text in the Cell C3 is " & Range("C3").Text
End Sub
19. Receiving two values using an Input Box and displaying their sum
using Message Box
Sub SumUsingInputBox()
a = Val(InputBox("Enter the value of A"))
b = Val(InputBox("Enter the value of B"))
c = a + b
MsgBox "Sum of A and B is " & c
End Sub
Sub WriteToTextFile()
Open "E:\XLnCAD.txt" For Output As 1
Range("D5").Select
Do Until ActiveCell.Value = ""
Print #1, ActiveCell.Value
ActiveCell.Offset(1, 0).Select
Loop
Close (1)
End Sub
Sub readfromtextfile()
Dim temptext As String
Open "E:\XLnCAD.txt" For Input As 2
Range("I6").Select
Do Until EOF(2)
Input #2, temptext
ActiveCell.Value = temptext
ActiveCell.Offset(1, 0).Select
Loop
Close (2)
End Sub
Sub HideSheets()
Dim XLnCAD As Worksheet
For Each XLnCAD In ThisWorkbook.Worksheets
If XLnCAD.Name <> ThisWorkbook.ActiveSheet.Name Then
XLnCAD.Visible = xlSheetHidden
End If
Next XLnCAD
End Sub
Sub UnHideSheets()
Dim XLnCAD As Worksheet
For Each XLnCAD In ActiveWorkbook.Worksheets
XLnCAD.Visible = xlSheetVisible
Next XLnCAD
End Sub
Sub VeryHiddenSheet()
Sheets("xlncad").Visible = xlVeryHidden
End Sub
Sub SelectedSheetsVeryHidden()
Dim Ws As Worksheet
For Each Ws In ActiveWindow.SelectedSheets
Ws.Visible = xlSheetVeryHidden
Next Ws
End Sub
Sub UnHideVeryHiddenSheets()
Dim Ws As Worksheet
For Each Ws In ActiveWorkbook.Worksheets
If Ws.Visible = xlSheetVeryHidden Then
Ws.Visible = xlSheetVisible
End If
Next Ws
End Sub
Sub DeleteSheets()
Dim XLnCAD As Worksheet
For Each XLnCAD In ThisWorkbook.Worksheets
If XLnCAD.Name <> ThisWorkbook.ActiveSheet.Name Then
Application.DisplayAlerts = False
XLnCAD.Delete
Application.DisplayAlerts = True
End If
Next XLnCAD
End Sub
Sub CreatePdf()
Dim ID As String
ID = Range("D4").Text
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="E:\Payslip\" + ID + ".pdf", _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub
Sub UpperCase()
For Each XLnCAD In Selection
XLnCAD.Value = UCase(XLnCAD.Value)
Next
End Sub
Sub LowerCase()
For Each XLnCAD In Selection
XLnCAD.Value = LCase(XLnCAD.Value)
Next
End Sub
Sub ProperCase()
For Each XLnCAD In Selection
XLnCAD.Value = Application.Proper(XLnCAD.Value)
Next
End Sub
Sub JoinStringsInColumn()
Dim XLnCAD As Range, Sonder As Range, XNC As String
Set XLnCAD = Selection
For Each Sonder In XLnCAD
XNC = XNC & " " & Sonder
Next
ActiveCell.Offset(0, 1).Select
Selection.Value = XNC
Selection.Font.Bold = True
End Sub
Sub JoinStringsInRow()
Dim XLnCAD As Range, Sonder As Range, XNC As String
Set XLnCAD = Selection
For Each Sonder In XLnCAD
XNC = XNC & " " & Sonder
Next
ActiveCell.Offset(1, 0).Select
Selection.Value = XNC
Selection.Font.Bold = True
End Sub
Sub HideRows()
Rows("5:10").EntireRow.Hidden = True
End Sub
Sub UnHideRows()
Rows("5:10").EntireRow.Hidden = False
End Sub
Sub UnhideAllRows()
Cells.EntireRow.Hidden = False
End Sub
Sub HideColumns()
Columns("B:D").EntireColumn.Hidden = True
End Sub
Sub UnHideColumns()
Columns("B:D").EntireColumn.Hidden = False
End Sub
Sub UnhideAllColumns()
Cells.EntireColumn.Hidden = False
End Sub
Sub Select1000Cells()
Range("A1:A1000").Select
End Sub
Sub Select40Cells()
Range("D10:K23").Select
End Sub
Sub ProtectEveryWorksheet()
Dim X As Worksheet
Sonder = InputBox("Enter the Password to protect the
worksheets", "Protect worksheets")
For Each X In ActiveWorkbook.Worksheets
X.Protect Password:=Sonder
Next X
End Sub
Sub UnProtectEveryWorksheet()
Dim Pass As String
Dim Y As Worksheet
Pass = InputBox("Enter the Password to Unprotect the
worksheets", "UnProtect worksheets")
If Pass = Sonder Then
For Each Y In ActiveWorkbook.Worksheets
Y.Unprotect Password:=Sonder
Next Y
Else
MsgBox "Incorrect Password"
End If
End Sub
Sub TextToSpeech()
Selection.Speak
End Sub
Sub AutoFitColumns()
Cells.EntireColumn.AutoFit
End Sub
Sub AutoFitRows()
Cells.EntireRow.AutoFit
End Sub
Sub ReverseStringsInASelection()
Dim X As Range
Dim Y As Integer
Y = 0
Set X = Selection
For Each X In Selection
ActiveCell.Offset(Y, 1).Value = StrReverse(X)
Y = Y + 1
Next X
End Sub
Sub PasteAsValuesSameSheet()
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = True
End Sub
Sub PasteAsValuesToNewSheet()
Cells.Select
Selection.Copy
Sheets.Add After:=ActiveSheet
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues
Selection.PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
End Sub
Sub PasteAsValuesEveryWorksheet()
Dim X As Worksheet
Dim Y As String
For Each X In ThisWorkbook.Sheets
Y = X.Name
Cells.Select
Selection.Copy
Sheets.Add After:=ActiveSheet
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues
Selection.PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
Sheets(Y).Select
Next X
End Sub
Sub ListEverySheet()
Dim XNC As Worksheet
Dim X As Integer
X = 0
Sheets("List of WorkSheets").Select
Range("A2").Select
For Each XNC In ThisWorkbook.Sheets
ActiveCell.Offset(X, 0).Value = XNC.Name
X = X + 1
Next
End Sub
Sub ListEverySheetWithProperties()
Dim XNC As Worksheet
Dim X As Integer
X = 0
Sheets("List of WorkSheets").Select
Range("A2").Select
For Each XNC In ThisWorkbook.Sheets
ActiveCell.Offset(X, 0).Value = XNC.Name
If XNC.Visible = True Then
ActiveCell.Offset(X, 1).Value = "Visible"
ElseIf XNC.Visible = xlSheetHidden Then
ActiveCell.Offset(X, 1).Value = "Hidden"
ElseIf XNC.Visible = xlSheetVeryHidden Then
ActiveCell.Offset(X, 1).Value = "Very Hidden"
End If
X = X + 1
Next
End Sub
Sub CountSheets()
MsgBox Application.Sheets.Count
End Sub
• By default, the Developer tab is not displayed in excel. If you can't see the
Developer tab, use Customize Ribbon option to activate it.
• We cannot Undo an action performed using a Macro. Make sure that you have
a backup of the data before executing the macro.
• To save macros in a workbook, you must save your workbook in a Macro-
Enabled format (*.xlsm).
• Use the comment option of VBA editor while writing the code for a Macro.
This will help you understand your own work if you have to revisit it weeks or
months later.