You are on page 1of 66

eBook on VBA for MS Excel

51 Ready to Use Macros


by Ajay Anand
This eBook contains, 51 ready to use Macros for MS Excel. How to use these codes and
execute the program are explained in the beginning of this book. Detailed explanations on
these codes are available in the YouTube channel of XL n CAD and the link of the related
video is shared under each code.

Version 1.0 For Video Tutorials and more, visit xlncad.com


51 Ready to Use Macros 1|Page

“It’s really clear that the most precious resource we all have is Time”
Steve Jobs

Macros is about saving Time.

If you learn something that saves you 10 Minutes Every Day,


that will result in more than 60 Hours of Time saved Every Year.

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 2|Page

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

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 3|Page

Introduction to VBA for Excel


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.

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 4|Page

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.

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 5|Page

How to use codes shared here?


1. Copy the Code.
2. Click on Visual Basic in the Developer Tab of Excel Ribbon. ALT + F11 is the
Shortcut for Visual Basic Editor.

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 6|Page

3. Right click on the tree view and Insert a Module.

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 7|Page

4. Paste the copied Code into the new module.

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 8|Page

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.

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 9|Page

List of 51 Ready to use Macros


1. Add a new Worksheet _________________________________________________________ 13
2. Add two new Worksheets into a Workbook ________________________________________ 14
3. Add Worksheets based on user input _____________________________________________ 15
4. Add and Name the Worksheets based on user Input _________________________________ 16
5. Create Worksheets from the names present in a Worksheet ___________________________ 17
6. Delete Worksheet ____________________________________________________________ 18
7. Delete Worksheet based on user input ____________________________________________ 19
8. Insert a single row in between each row containing data _____________________________ 20
9. Insert 2 Rows in between each row containing data _________________________________ 21
10. Insert Rows in between each row containing data, based on user Input ________________ 22
11. Delete blank Rows in between rows containing data _______________________________ 23
12. Delete blank Rows (2 rows each) in between rows containing data ____________________ 24
13. Insert Columns in between each column containing data ____________________________ 25

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 10 | P a g e

14. Delete Columns in between columns containing data _______________________________ 26


15. Display a Message using Message Box __________________________________________ 27
16. Defining the Title of the Message Box ___________________________________________ 28
17. Display a Message using Value in an Excel Sheet __________________________________ 29
18. Display a Message using Text in an Excel Sheet____________________________________ 30
19. Receiving two values using an Input Box and displaying their sum using Message Box _____ 31
20. Export data (Write) from a Worksheet to a Text File ________________________________ 32
21. Import data (Read) from a Text File to a Worksheet ________________________________ 33
22. Hide All Worksheets in a workbook (Except Active sheet) ____________________________ 34
23. Unhide Every Hidden sheet in an Excel Workbook __________________________________ 35
24. Convert a Worksheet to Very Hidden Sheet Mode __________________________________ 36
25. Unhide a Very Hidden Sheet ___________________________________________________ 37
26. Delete All Worksheets in a workbook (Except Active sheet) __________________________ 38
27. Create a PDF file from a Worksheet _____________________________________________ 39
28. Convert text into Upper Case __________________________________________________ 40
xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 11 | P a g e

29. Convert text into Lower Case __________________________________________________ 41


30. Convert text into Proper Case __________________________________________________ 42
31. Join Text present in every cell of a Column _______________________________________ 43
32. Join Text present in every cell of a Row __________________________________________ 44
33. Hide Specific Row/Rows of a Worksheet _________________________________________ 45
34. Unhide Specific Row/Rows of a Worksheet _______________________________________ 46
35. Unhide Every hidden Row of a Worksheet ________________________________________ 47
36. Hide Specific Column/Columns of Worksheet _____________________________________ 48
37. Unhide Specific Column/Columns of a Worksheet __________________________________ 49
38. Unhide Every hidden Column of a Worksheet _____________________________________ 50
39. Select first 1000 cells from first 4 columns A, B, C & D of a Worksheet __________________ 51
40. Protect Every Worksheet in a Workbook with a password ___________________________ 52
41. Unprotect Every protected Worksheet in a Workbook ______________________________ 53
42. Text to Speech _____________________________________________________________ 54
43. Unmerge all merged cells in a Worksheet ________________________________________ 55
xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 12 | P a g e

44. AutoFit Rows_______________________________________________________________ 56


45. AutoFit Columns ____________________________________________________________ 57
46. Reverse A String ____________________________________________________________ 58
47. Copy the data from a Worksheet and Paste as Values to a New Sheet __________________ 59
48. Count the total number of Worksheets in a Workbook ______________________________ 60
49. Get the name of every Worksheet present in a Workbook ___________________________ 61
50. Count number of Rows and Columns in a selection _________________________________ 62
51. Find largest number in a selection ______________________________________________ 63

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 13 | P a g e

1. Add a new Worksheet

Sub AddNewSheet()
Sheets.Add
End Sub

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 14 | P a g e

2. Add two new Worksheets into a Workbook

Sub AddMultipleSheets ()
Sheets.Add(, , 2)
End Sub

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 15 | P a g e

3. Add Worksheets based on user input

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

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 16 | P a g e

4. Add and Name the Worksheets based on user Input

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

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 17 | P a g e

5. Create Worksheets from the names present in a Worksheet

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

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 18 | P a g e

6. Delete Worksheet

Sub DeleteSheet()
Sheets(“Sheet1”).Delete
End Sub

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 19 | P a g e

7. Delete Worksheet based on user input

Sub DeleteSheetUserInput()
Dim XLnCAD As String
XLnCAD = Val(InputBox(“Enter the name of the sheet to be deleted”))
Sheets(XLnCAD).Delete
End Sub

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 20 | P a g e

8. Insert a single row 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

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 21 | P a g e

9. Insert 2 Rows in between each row containing data

Sub Insert2Rows()
Range(“A2”).Select
Do Until ActiveCell.Value = “”
Selection.EntireRow.Insert
Selection.EntireRow.Insert
ActiveCell.Offset(3, 0).Select
Loop
End Sub

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 22 | P a g e

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

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 23 | P a g e

11. Delete blank Rows in between rows containing data

Sub DeleteRows()
Range(“A2”).Select
Do Until ActiveCell.Offset(1, 0).Value = “”
Selection.EntireRow.Delete
ActiveCell.Offset(1, 0).Select
Loop
End Sub

Watch the video in YouTube

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 24 | P a g e

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

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 25 | P a g e

13. Insert Columns in between each column containing data

Sub InsertColumns()
Range(“B1”).Select
Do Until ActiveCell.Value = “”
Selection.EntireColumn.Insert
ActiveCell.Offset(0, 2).Select
Loop
End Sub

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 26 | P a g e

14. Delete Columns in between columns containing data

Sub DeleteColumns()
Range(“B1”).Select
Do Until ActiveCell.Offset(0, 1).Value = “”
Selection.EntireColumn.Delete
ActiveCell.Offset(0, 1).Select
Loop
End Sub

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 27 | P a g e

15. Display a Message using Message Box

Sub SimpleMessageBox()
MsgBox “Hi Everyone”
End Sub

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 28 | P a g e

16. Defining the Title of the Message Box

Sub MessageBoxTitle()
MsgBox “Hi Everyone. Greetings from XL n CAD”, , “XL n CAD”
End Sub

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 29 | P a g e

17. Display a Message using Value in an Excel Sheet

Sub DisplayValueFromSheet()
MsgBox “The value of cell A1 is ” & Range(“A1”).Value
End Sub

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 30 | P a g e

18. Display a Message using Text in an Excel Sheet

Sub DisplayTextFromSheet ()
MsgBox “The in the Cell C3 is ” & Range(“C3”).Text
End Sub

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 31 | P a g e

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

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 32 | P a g e

20. Export data (Write) from a Worksheet to a Text File

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

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 33 | P a g e

21. Import data (Read) from a Text File to a Worksheet

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

22. Hide All Worksheets in a workbook (Except Active sheet)

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

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 35 | P a g e

23. Unhide Every Hidden sheet in an Excel Workbook

Sub UnHideSheets()
Dim XLnCAD As Worksheet
For Each XLnCAD In ActiveWorkbook.Worksheets
XLnCAD.Visible = xlSheetVisible
Next XLnCAD
End Sub

Watch the video in YouTube

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 36 | P a g e

24. Convert a Worksheet to Very Hidden Sheet Mode

Sub VeryHiddenSheet()
Sheets("XLnCAD").Visible = xlVeryHidden
End Sub

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 37 | P a g e

25. Unhide a Very Hidden Sheet

Sub UnHideSheets()
Dim XLnCAD As Worksheet
For Each XLnCAD In ActiveWorkbook.Worksheets
XLnCAD.Visible = xlSheetVisible
Next XLnCAD
End Sub

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 38 | P a g e

26. Delete All Worksheets in a workbook (Except Active sheet)

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

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 39 | P a g e

27. Create a PDF file from a Worksheet

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

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 40 | P a g e

28. Convert text into Upper Case

Sub UpperCase()
For Each XLnCAD In Selection
XLnCAD.Value = UCase(XLnCAD.Value)
Next
End Sub

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 41 | P a g e

29. Convert text into Lower Case

Sub LowerCase()
For Each XLnCAD In Selection
XLnCAD.Value = LCase(XLnCAD.Value)
Next
End Sub

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 42 | P a g e

30. Convert text into Proper Case

Sub ProperCase()
For Each XLnCAD In Selection
XLnCAD.Value = Application.Proper(XLnCAD.Value)
Next
End Sub

Watch the video in YouTube

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 43 | P a g e

31. Join Text present in every cell of a Column

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

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 44 | P a g e

32. Join Text present in every cell of a Row


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

Watch the video in YouTube

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 45 | P a g e

33. Hide Specific Row/Rows of a Worksheet

Sub HideRows()
Rows("5:10").EntireRow.Hidden = True
End Sub

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 46 | P a g e

34. Unhide Specific Row/Rows of a Worksheet

Sub UnHideRows()
Rows("5:10").EntireRow.Hidden = False
End Sub

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 47 | P a g e

35. Unhide Every hidden Row of a Worksheet

Sub UnhideAllRows()
Cells.EntireRow.Hidden = False
End Sub

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 48 | P a g e

36. Hide Specific Column/Columns of Worksheet

Sub HideColumns()
Columns("B:D").EntireColumn.Hidden = True
End Sub

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 49 | P a g e

37. Unhide Specific Column/Columns of a Worksheet

Sub UnHideColumns()
Columns("B:D").EntireColumn.Hidden = False
End Sub

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 50 | P a g e

38. Unhide Every hidden Column of a Worksheet

Sub UnhideAllColumns()
Cells.EntireColumn.Hidden = False
End Sub

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 51 | P a g e

39. Select first 1000 cells from first 4 columns A, B, C & D of a Worksheet

Sub SelectCells()
Range("A1:D1000").Select
End Sub

Watch the video in YouTube


xlncad.com Visit XL n CAD’s Youtube Channel
51 Ready to Use Macros 52 | P a g e

40. Protect Every Worksheet in a Workbook with a password

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

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 53 | P a g e

41. Unprotect Every protected Worksheet in a Workbook

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

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 54 | P a g e

42. Text to Speech

Sub TextToSpeech()
Selection.Speak
End Sub

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 55 | P a g e

43. Unmerge all merged cells in a Worksheet

Sub UnmergeAllCells()
ActiveSheet.Cells.UnMerge
End Sub

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 56 | P a g e

44. AutoFit Rows

Sub AutoFitRows()
Cells.EntireRow.AutoFit
End Sub

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 57 | P a g e

45. AutoFit Columns

Sub AutoFitColumns()
Cells.EntireColumn.AutoFit
End Sub

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 58 | P a g e

46. Reverse A String

Sub ReverseString()
Dim XLnCAD As String
XLnCAD = Range("A1").Value
Range("A2").Value = StrReverse(XLnCAD)
End Sub

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 59 | P a g e

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

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 60 | P a g e

48. Count the total number of Worksheets in a Workbook

Sub CountSheets()
MsgBox Application.Sheets.Count
End Sub

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 61 | P a g e

49. Get the name of every Worksheet present in a Workbook

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

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 62 | P a g e

50. Count number of Rows and Columns in a selection

Sub CountRowAndColumns()
MsgBox "Rows " & Selection.Rows.Count _
& vbCrLf & "Columns " & Selection.Columns.Count
End Sub

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 63 | P a g e

51. Find largest number in a selection

Sub LargetstNumber()
Dim XLnCAD As Double, Sonder As Range
Set Sonder = Selection
XLnCAD = WorksheetFunction.Max(Sonder)
MsgBox XLnCAD
End Sub

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 64 | P a g e

Things to remember while using Macros


• 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.

xlncad.com Visit XL n CAD’s Youtube Channel


51 Ready to Use Macros 65 | P a g e

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

xlncad.com Visit XL n CAD’s Youtube Channel

You might also like