Professional Documents
Culture Documents
51 Ready To Use Excel Macros V 1.0 070419 PDF
51 Ready To Use Excel Macros V 1.0 070419 PDF
“It’s really clear that the most precious resource we all have is Time”
Steve Jobs
Hi,
I am Ajay Anand, a Civil Engineer by education, Highway Engineer by
profession, Coder by interest and occasionally a Blogger.
I started XL n CAD as an effort to learn and share Tips, Shortcuts and
Tutorials related to MS Excel & AutoCAD.
I am pumping into XL n CAD, every piece of information and
knowledge which I acquired during my Highway Designing career
spanning around 11 years. I would like you to be a part of this
knowledge sharing process and make it a wonderful experience.
In this ebook, I share 51 ready to use Macros for MS Excel that will help you to get more productive in
your work. Detailed explanations of these codes are available in my YouTube Channel.
For more such tips and tricks regarding MS Excel and AutoCAD, visit my blog http://xlncad.com.
Also, feel free to share this eBook with your friends and colleagues. For queries drop me a mail at
admin@xlncad.com
Thanks & Regards
Ajay Anand
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 feature of an Excel macro is, it can be
saved for future use. It can also 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. ALT +F8 is the shortcut for
activating Macro Dialog Box. Select a Macro, Click on Run button to execute
the macro.
Sub AddNewSheet()
Sheets.Add
End Sub
Sub AddMultipleSheets ()
Sheets.Add(, , 2)
End Sub
Sub AddSheetsUserInput ()
Dim i as integer, 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 as integer, 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
6. Delete Worksheet
Sub DeleteSheet()
Sheets(“Sheet1”).Delete
End Sub
Sub DeleteSheetUserInput()
Dim XLnCAD As String
XLnCAD = Val(InputBox(“Enter the name of the sheet to be deleted”))
Sheets(XLnCAD).Delete
End Sub
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(“A3”).Select
Dim d as integer, 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 Everyone”
End Sub
Sub MessageBoxTitle()
MsgBox “Hi Everyone. Greetings from XL n CAD”, , “XL n CAD”
End Sub
Sub DisplayValueFromSheet()
MsgBox “The value of cell A1 is ” & Range(“A1”).Value
End Sub
Sub DisplayTextFromSheet ()
MsgBox “The 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 = InputBox(“Enter the value of A”)
b = InputBox(“Enter the value of B”)
c=a+b
MsgBox “Sum of A and B is ” & c
End Sub
Sub WriteToTextFile()
Open "D:\XLnCAD.txt" For Output As 1
Range("A1").Select
Do Until ActiveCell.Value = ""
Print #1, ActiveCell.Value
ActiveCell.Offset(1, 0).Select
Loop
Close (1)
End Sub
Sub ReadFromTextFile()
Dim XLnCAD As String
Open "D:\XLnCAD.txt" For Input As 1
Range("C1").Select
For i = 1 To 3
Input #2, XLnCAD
Next i
ActiveCell.Value = XLnCAD
Close (1)
End Sub
Watch the video in YouTube
xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 34 | P a g e
Sub HideAllWorksheets()
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 UnHideSheets()
Dim XLnCAD As Worksheet
For Each XLnCAD In ActiveWorkbook.Worksheets
XLnCAD.Visible = xlSheetVisible
Next XLnCAD
End Sub
Sub DeleteWorksheets()
Dim XLnCAD As Worksheet
For Each XLnCAD In ThisWorkbook.Worksheets
If XLnCAD.Name <> ThisWorkbook.ActiveSheet.Name Then
Application.DisplayAlerts = False
XLnCAD.Delete
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
Watch the video in YouTube
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
39. Select first 1000 cells from first 4 columns A, B, C & D of a Worksheet
Sub SelectCells()
Range("A1:D1000").Select
End Sub
Sub ProtectAllWorksheets()
Dim XLnCAD As Worksheet
Dim Sonder As String
Sonder = InputBox("Enter a Password.", "XL n CAD")
For Each XLnCAD In ActiveWorkbook.Worksheets
XLnCAD.Protect Password:=Sonder
Next XLnCAD
End Sub
Sub UnProtectAllSheets()
Dim XLnCAD As Worksheet
Dim Sonder As String
Sonder = "Password"
'this password should be the same which you used to protect the sheets
For Each XLnCAD In Worksheets
XLnCAD.Unprotect password:=Sonder
Next XLnCAD
End Sub
Sub TextToSpeech()
Selection.Speak
End Sub
Sub UnmergeAllCells()
ActiveSheet.Cells.UnMerge
End Sub
Sub AutoFitRows()
Cells.EntireRow.AutoFit
End Sub
Sub AutoFitColumns()
Cells.EntireColumn.AutoFit
End Sub
Sub ReverseString()
Dim XLnCAD As String
XLnCAD = Range("A1").Value
Range("A2").Value = StrReverse(XLnCAD)
End Sub
47. Copy the data from a Worksheet and Paste as Values to a New Sheet
Sub PasteAsValuesToNewSheet()
Cells.Select
Selection.Copy
Sheets.Add After:=ActiveSheet
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues
End Sub
Sub CountSheets()
MsgBox Application.Sheets.Count
End Sub
Sub ListEverySheet()
Dim XNC As Worksheet
Dim x As Integer
x=1
Sheets("XL n CAD").Range("A:A").Clear
For Each XNC In Worksheets
Sheets("XL n CAD").Cells(x, 1) = XNC.Name
x=x+1
Next XNC
End Sub
Sub CountRowAndColumns()
MsgBox "Rows " & Selection.Rows.Count _
& vbCrLf & "Columns " & Selection.Columns.Count
End Sub
Sub LargetstNumber()
Dim XLnCAD As Double, Sonder As Range
Set Sonder = Selection
XLnCAD = WorksheetFunction.Max(Sonder)
MsgBox XLnCAD
End Sub
Hi,
I am Ajay Anand, a Civil Engineer by education, Highway Engineer by
profession, Coder by interest and occasionally a Blogger.
I started XL n CAD as an effort to learn and share Tips, Shortcuts and
Tutorials related to MS Excel & AutoCAD.
I am pumping into XL n CAD, every piece of information and
knowledge which I acquired during my Highway Designing career
spanning around 11 years. I would like you to be a part of this
knowledge sharing process and make it a wonderful experience.
In this ebook, I share 51 ready to use Macros for MS Excel that will help you to get more productive in
your work. Detailed explanations of these codes are available in my YouTube Channel.
For more such tips and tricks regarding MS Excel and AutoCAD, visit my blog http://xlncad.com.
Also, feel free to share this eBook with your friends and colleagues. For queries drop me a mail at
admin@xlncad.com
Thanks & Regards
Ajay Anand