Professional Documents
Culture Documents
Macro codes can save you a ton of time. You can automate small as well as heavy tasks with
VBA codes.
Awesome
And, do you know with the help ofmacros, you can break all the limitations of excel which you
Formu
SUMIFS w
think excel has?
Calculate
Condition
So, today, I have listed some of the useful codes to help youbecome more productive in your Count Un
day to day work.
SUMPROD
Charti
You can use these macro codes even if you haven't used VBA before that. All you have to do Milestone
just paste these useful macros codes in your VBA editor. Populatio
Waffle Ch
These codes will exactly do the same thing which headings are telling you. For your Thermom
convenience, please follow these steps to add these codes to your workbook. Excel Piv
VBA
VBA To C
First of all, make sure you have your developer tab on your excel ribbon. If you don't have
ON-OFF B
please use these simple steps to activate developer tab.
VBA IF S
Useful Ma
Go to your developer tab and click on "Visual Basic".
90% Dis
516 Shares = Share w Share Share m Pin
On the le side in "Project Window", right click on the name of your workbook and insert
a new module.
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 1/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Just paste your codes into the module and close it.
It will show you a window with a list of the macros you have in your le.
3. hide all but the active worksheet 54. convert date into year
5. Delete All but the Active Worksheet 56. remove date from date & time
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 2/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
11. Hide All Pivot Table Subtotals 63. convert to sentence case
12. Refresh All Pivot Tables 64. remove a character from selection
14. Highlight the Active Row and Column 66. remove the apostrophe from a number
17. Remove Characters from a string 69. remove decimals from numbers
18. Active Workbook in an Email 70. multiply all the values by a number
19. Convert Range into an image 71. add a number in all the numbers
23. Insert Multiple Worksheets 75. highlight alternate rows in the selection
-
24. Highlight Named Ranges 76. highlight cells with misspelled words
25. Highlight Greater than Values 77. protect all the cells with formulas
26. Highlight Lower than Values 78. add a-z alphabets in a range
29. Convert Text To Upper Case 81. convert Roman numbers into Arabic
number
30. Convert Text To Lower Case
82. use goal seek
31. Insert Multiple Columns
516 Shares = Share w Share
83. unhideShare
all rowsm Pincolumns
and
32. Insert Multiple Rows
84. save each worksheet as a single pdf
33. Auto Fit Columns
85. count/highlight cells with error in entire
34. Auto Fit Rows
worksheet
35. Remove Text Wrap
86. count/highlight cells with a speci c in
36. Unmerge Cells entire worksheet
37. Change Chart Type 87. highlight all the cells in the worksheet
38. Paste Chart as an Image which are blank but have an invisible space
39. Add Chart Title 88. highlight max value in the range
42. Add Workbook to a mail Attachment 91. show progress on status bar
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 3/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
And, it will also add the current date with the name of the le.
Sub FileBackUp()
ThisWorkbook.name
End Sub
516 Shares = Share w Share Share m Pin
If any of the worksheets is not saved, you'll get a message to save it.
Sub CloseAllWorkbooks()
wbs.Close SaveChanges:=True
Next wb
End Sub
Sub HideWorksheet()
Dim ws As Worksheet
ws.Visible = xlSheetHidden
End If
Next ws
End Sub
Sub UnhideAllWorksheet()
Dim ws As Worksheet
ws.Visible = xlSheetVisible
Next ws
End Sub
When you run this macro it will compare the name of the active worksheet with other
worksheets and then delete them.
Sub DeleteWorksheets()
Dim ws As Worksheet
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
End If
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 5/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Next ws
End Sub
Sub CopyWorksheetToNewWorkbook()
ThisWorkbook.ActiveSheet.Copy _
Before:=Workbooks.Add.Worksheets(1)
End Sub
When you run this macro, you will get an input box to enter a password. Once you enter your
password, click OK. And, make sure to take care about CAPS.
Sub ProtectAllWorskeets()
Dim ws As Worksheet
Dim ps As String
ws.Protect Password:=ps
Next ws
End Sub
Sub ConvertToValues()
Select Case MsgBox("You Can't Undo This Action. " & "Save Workbook First?", vbYesNoCancel,
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 6/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
"Alert")
Case Is = vbYes
ThisWorkbook.Save
Case Is = vbCancel
Exit Sub
End Select
If MyCell.HasFormula Then
MyCell.Formula = MyCell.Value
End If
Next MyCell
End Sub
Sub RemoveSpaces()
Select Case MsgBox("You Can't Undo This Action. " & "Save Workbook First?", _
vbYesNoCancel, "Alert")
Case Is = vbYesThisWorkbook.Save
End Select
myCell = Trim(myCell)
End If
Next myCell
End Sub
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 7/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Sub HighlightDuplicateValues()
myCell.Interior.ColorIndex = 36
End If
Next myCell
End Sub
Sub HideSubtotals()
Dim pt As PivotTable
Dim pf As PivotField
Set pt = ActiveSheet.PivotTables(ActiveCell.PivotTable.name)
If pt Is Nothing Then
Exit Sub
End If
516 Shares
For Each pf In pt.PivotFields = Share w Share Share m Pin
pf.Subtotals(1) = True
pf.Subtotals(1) = False
Next pf
End Sub
Sub RefreshAllPivotTables()
Dim ws As Worksheet
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 8/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Dim pt As PivotTable
pt.RefreshTable
Next pt
Next ws
End Sub
Sub Resize_Charts()
Dim i As Integer
For i = 1 To ActiveSheet.ChartObjects.Count
With ActiveSheet.ChartObjects(i)
.Width = 300
.Height = 200
End With
Next i
End Sub
I really love this macro code whenever I have to analyze a data table.
Go to Project Explorer (Ctrl + R, If hidden). Select your workbook & double click on the
name of a particular worksheet in which you want to activate the macro.
Paste the code into it & Select the BeforeDoubleClick from event drop down menu.
Remember that, by applying this macro you will not able to edit the cell by double click.
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 9/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Target.Cells.EntireRow.Address
Range(strRange).Select
End Sub
Sub SaveAsPDF()
End Sub
Don't worry this macro code will rescue everything. When you run this code it will create a
new worksheet and list the name of all worksheets with a hyperlink to them.
Sub TableofContent()
Dim i As Long
Application.DisplayAlerts = False
Worksheets("Table of Content").Delete
Application.DisplayAlerts = True
On Error GoTo 0
ThisWorkbook.Sheets.Add Before:=ThisWorkbook.Worksheets(1)
For i = 1 To Sheets.Count
With ActiveSheet
.Hyperlinks.Add _
Anchor:=ActiveSheet.Cells(i, 1), _
Address:="", _
ScreenTip:=Sheets(i).Name, _
TextToDisplay:=Sheets(i).Name
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 10/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
End With
Next i
End Sub
It has two arguments "rng" for the text string and "cnt" for the count of characters to remove.
For example: If you want to remove rst characters from a cell, you need to enter 1 incnt.
End Function
You can change the subject, email, and body text in code. And if you want to send this mail
directly, use ".Send" instead of ".Display".
With OutMail
.to = "Sales@FrontLinePaper.com"
.Attachments.Add ActiveWorkbook.FullName
.display
End With
End Sub
Sub PasteAsPicture()
Application.CutCopyMode = False
Selection.Copy
ActiveSheet.Pictures.Paste.Select
End Sub
Sub LinkedPicture()
Selection.Copy
ActiveSheet.Pictures.Paste(Link:=True).Select
End Sub
Selection.FormatConditions.AddTop10
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1)
.TopBottom = xlTop10Top
.Rank = 10
.Percent = False
End With
With Selection.FormatConditions(1).Font
.Color = -16752384
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13561798
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 12/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
A er that, it will instantly insert serial numbers starting from the active cell.
Sub AddSerialNumbers()
Dim i As Integer
For i = 1 To i
ActiveCell.Value = i
ActiveCell.Offset(1, 0).Activate
Next i
Last:
Exit Sub
End Sub
You can use this code if you want to add multiple worksheets in your workbook in a single
shot. When you run this macro code you will get an input box to enter the total number of
sheets you want to enter.
Sub InsertMultipleSheets()
Dim i As Integer
End Sub
If you are not sure about how many named ranges you have in your worksheet then you can
use this code to highlight all of them.
Sub HighlightRanges()
HighlightRange.Interior.ColorIndex = 36
Next RangeName
End Sub
Once you run this code it will ask you for the value from which you want to highlight all
greater values.
Sub HighlightGreaterThanValues()
Dim i As Integer
516 Shares
Selection.FormatConditions.Delete = Share w Share Share m Pin
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:=i
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1)
.Font.Color = RGB(0, 0, 0)
End With
End Sub
Once you run this code it will ask you for the value from which you want to highlight all lower
values.
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 14/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Sub HighlightLowerThanValues()
Dim i As Integer
Selection.FormatConditions.Delete
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1)
.Font.Color = RGB(0, 0, 0)
End With
End Sub
If you want to protect your worksheet you can use this macro code. All you have to do just
mention your password in the code.
Sub ProtectWS()
End Sub
If you want to unprotect your worksheet you can use this macro code. All you have to do just
mention your password which you have used while protecting your worksheet.
Sub UnprotectWS()
ActiveSheet.Unprotect "mypassword"
End Sub
This code will help you to convert your text into upper case text in a click.
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 15/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Sub ConvertUpperCase()
rng = UCase(rng)
Next rng
End Sub
This code will help you to convert your text into lower case text in a click.
Sub ConvertLowerCase()
rng = LCase(rng)
Next rng
End Sub
Once you run this macro it will show an input box and you need to enter the number of
516 Shares
columns you want to insert. = Share w Share Share m Pin
Sub InsertMultipleColumns()
Dim i As Integer
Dim j As Integer
ActiveCell.EntireColumn.Select
For j = 1 To i
Next j
Last:
Exit Sub
End Sub
Once you run this macro it will show an input box and you need to enter the number of rows
you want to insert.
Sub InsertMultipleRows()
Dim i As Integer
Dim j As Integer
ActiveCell.EntireRow.Select
For j = 1 To i
Next j
Last:
Exit Sub
End Sub
This macro code will select all the cells in your worksheet and instantly auto t all the columns.
Cells.Select
Cells.EntireColumn.AutoFit
End Sub
This macro code will select all the cells in your worksheet and instantly auto t all the row.
Sub AutoFitRows()
Cells.Select
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 17/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Cells.EntireRow.AutoFit
End Sub
This code will help you to remove text wrap from all the worksheet in a single click. It will rst
select all the columns and then remove text wrap.
Sub RemoveWrapText()
Cells.EntireRow.AutoFit
Cells.EntireColumn.AutoFit
End Sub
Select your cells and run this code. It will unmerge all the cells from the selection.
Sub UnmergeCells()
Selection.UnMerge
End Sub
516 Shares = Share w Share Share m Pin
This code will help you to convert chart type without using chart options from the tab. All you
have to do just specify to which type you want to convert.
Below code will convert selected chart to a clustered column chart. There are di erent codes
for di erent types, you can nd all those types from here.
Sub ChangeChartType()
ActiveChart.ChartType = xlColumnClustered
End Sub
This code will help you to convert your chart into an image. You just need to select your chart
and run this code.
Sub ConvertChartToPicture()
ActiveChart.ChartArea.Copy
ActiveSheet.Range("A1").Select
ActiveSheet.Pictures.Paste.Select
End Sub
First of all, you need to select your chart and the run this code. You will get an input box to
enter chart title.
Sub AddChartTitle()
Dim i As Variant
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.ChartTitle.Text = i
516 Shares = Share w Share Share m Pin
Last:
Exit Sub
End Sub
All you have to do just enter "rvrse" function in a cell and refer to the cell in which you have
text which you want to reverse.
rvrse = VBA.strReverse(cell.Value)
End Function
This code will help you to sort worksheets in your workbook according to their name.
Sub SortWorksheets()
Dim i As Integer
Dim j As Integer
For i = 1 To Sheets.Count
For j = 1 To Sheets.Count - 1
Sheets(j).Move After:=Sheets(j + 1)
End If
Sheets(j).Move After:=Sheets(j + 1)
End If
End If
Next j
Once you run this macro it will open your default mail client and attached active workbook
with it as an attachment.
Sub OpenWorkbookAsAttachment()
Application.Dialogs(xlDialogSendMail).Show
End Sub
This macro code will help you to activate R1C1 reference style without using excel options.
Sub ActivateR1C1()
Application.ReferenceStyle = xlR1C1
Else
Application.ReferenceStyle = xlR1C1
End If
End Sub
This macro code will help you to activate A1 reference style without using excel options.
Sub ActivateA1()
Application.ReferenceStyle = xlA1
Else
Application.ReferenceStyle = xlA1
End If
End Sub
516 Shares = Share w Share Share m Pin
When you run this code it will open window calculator which you can use for your
calculations.
Sub OpenCalculator()
Application.ActivateMicrosoftApp Index:=0
End Sub
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 21/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Just select a range and run this code, excel will speak all the text what you have in that range,
cell by cell.
Sub Speak()
Selection.Speak
End Sub
There is a default user form in excel which you can use for data entry. And, you can use this
code to activate that user form.
Sub DataForm()
ActiveSheet.ShowDataForm
End Sub
With this code, you can insert a time stamp from 00:00 to 23:00.
Sub TimeStamp()
For i = 1 To 24
ActiveCell.Offset(RowOffset:=1, ColumnOffset:=0).Select
Next i
End Sub
You can create a pivot table with this code in seconds with this code.
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 22/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Sub InsertPivotTable()
'Declare Variables
'Delete Preivous Pivot Table Worksheet & Insert a New Blank Worksheet With Same Name
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
(SourceType:=xlDatabase, SourceData:=PRange). _
516 Shares = Share w Share
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _ Share m Pin
TableName:="SalesPivotTable")
With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Year")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Month")
.Orientation = xlRowField
.Position = 2
End With
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 23/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Zone")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("SalesPivotTable").PivotFields("Amount")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.NumberFormat = "#,##0"
End With
ActiveSheet.PivotTables("SalesPivotTable").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("SalesPivotTable").TableStyle2 = "PivotStyleMedium9"
End Sub
If you are not using Excel tables then you can use this code to update pivot table range.
Sub UpdatePivotTableRange()
PivotName = "PivotTable2"
Data_Sheet.Activate
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 24/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
LastCol = StartPoint.End(xlToRight).Column
DownCell = StartPoint.End(xlDown).Row
Pivot_Sheet.PivotTables(PivotName). _
ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:=NewRange)
Pivot_Sheet.PivotTables(PivotName).RefreshTable
'Complete Message
Pivot_Sheet.Activate
End Sub
Sub auto_open()
End Sub
Sub auto_close()
MsgBox "Bye Bye! Don't forget to check other cool stuff on excelchamps.com"
End Sub
Sub date2day()
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 25/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Selection.Value = Selection.Value
With tempCell
.Value = Day(tempCell)
.NumberFormat = "0"
End With
End If
Next tempCell
End Sub
Sub date2year()
Selection.Value = Selection.Value
With tempCell
.Value = Year(tempCell)
.NumberFormat = "0"
End With
End If
516 Shares = Share w Share Share m Pin
Next tempCell
End Sub
Sub removeTime()
Rng.Value = VBA.Int(Rng.Value)
End If
Next
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 26/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Selection.NumberFormat = "dd-mmm-yy"
End Sub
Sub removeDate()
End If
End Sub
Sub dateInHeader()
With ActiveSheet.PageSetup
.LeftHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
End With
ActiveWindow.View = xlNormalView
End Sub
To change the alignment of header or footer you can edit the code.
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 27/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Sub customHeader()
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = myText
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
End With
End Sub
Sub activateGetPivotData()
Application.GenerateGetPivotData = True
End Sub
Sub deactivateGetPivotData()
516 Shares = =
Application.GenerateGetPivotData Share
False w Share Share m Pin
End Sub
Sub convertUpperCase()
If Application.WorksheetFunction.IsText(Rng) Then
Rng.Value = UCase(Rng)
End If
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 28/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Next
End Sub
Just select a range of cells where you have text and run this code. If a cell has a number or any
value other than text that value will remain same.
Sub convertLowerCase()
If Application.WorksheetFunction.IsText(Rng) Then
Rng.Value= LCase(Rng)
End If
Next
End Sub
516 Shares
Sub convertProperCase() = Share w Share Share m Pin
Dim Rng As Range
If WorksheetFunction.IsText(Rng) Then
Rng.Value= WorksheetFunction.Proper(Rng.Value)
End If
Next
End Sub
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 29/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Sub convertTextCase()
If WorksheetFunction.IsText(Rng) Then
End If
Next rng
End Sub
Sub removeChar()
Dim rc As String
Next
End Sub
Sub relToAbs()
End If
Next c
End Sub
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 30/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
If you have numeric data where you have an apostrophe before each number, you run this
code to remove it.
Sub removeApostrophes()
Selection.Value = Selection.Value
End Sub
Sub highlightNegativeNumbers()
If WorksheetFunction.IsNumber(Rng) Then
Rng.Font.Color= -16776961
End If
End If
Next
End Sub
Sub highlightValue()
Dim I As Long
Dim J As Long
If ActiveWindow.RangeSelection.Count> 1 Then
myTxt= ActiveWindow.RangeSelection.AddressLocal
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 31/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Else
myTxt= ActiveSheet.UsedRange.AddressLocal
End If
Required", myTxt, , , , , 8)
Exit Sub
End If
GoTo LInput
End If
For I = 0 To myRg.Rows.Count-1
With myRg.Range("A1").Offset(I, 0)
.Font.ColorIndex= 1
For J = 1 To Len(.Text)
.Characters(J, Len(myStr)).Font.ColorIndex= 3
Next
End With
Next I
End Sub
Sub removeDecimals()
rng.Value= Int(rng)
rng.NumberFormat= "0"
Next rng
End Sub
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 32/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Select that range of cells and run this code. It will rst ask you for the number with whom you
want to multiple and then instantly multiply all the numbers with it.
Sub multiplyWithNumber()
If WorksheetFunction.IsNumber(rng) Then
rng.Value = rng * c
Else
End If
Next rng
End Sub
Sub addNumber() Dim rngAs Range DimiAs Integer i= InputBox("Enter number to multiple",
516 Shares = Share w Share Share m Pin
"Input Required") For Each rngIn Selection If WorksheetFunction.IsNumber(rng) Then
Sub getSquareRoot() Dim rngAs Range Dimi As Integer For Each rngIn Selection If
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 33/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Sub getCubeRoot()
Dimi As Integer
If WorksheetFunction.IsNumber(rng) Then
rng.Value = rng ^ (1 / 3)
Else
End If
Nextrng
End Sub
Sub highlightCommentCells()
Selection.SpecialCells(xlCellTypeComments).Select
Selection.Style= "Note"
End Sub
Sub highlightAlternateRows()
If rng.RowMod 2 = 1 Then
rng.Value= rng^ (1 / 3)
Else
End If
Next rng
End Sub
Sub HighlightMisspelledCells()
Next rng
End Sub
Sub lockCellsWithFormulas()
With ActiveSheet
.Unprotect
.Cells.Locked = False
.Cells.SpecialCells(xlCellTypeFormulas).Locked = True
.Protect AllowDeletingRows:=True
End With
End Sub
516 Shares = Share w Share Share m Pin
Sub addcAlphabets()
Dim i As Integer
For i= 65 To 90
ActiveCell.Value= Chr(i)
ActiveCell.Offset(1, 0).Select
Next i
End Sub
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 35/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Sub addsAlphabets()
Dim i As Integer
For i= 97 To 122
ActiveCell.Value= Chr(i)
ActiveCell.Offset(1, 0).Select
Next i
End Sub
Sub VisibleWorkbooks()
Dim i As Integer
i = i + 1
End If
Next book
MsgBox i
End Sub
Sub deleteBlankWorksheets()
Dim Ws As Worksheet
Application.ScreenUpdating= False
Application.DisplayAlerts= False
If Application.WorksheetFunction.CountA(Ws.UsedRange) = 0 Then
Ws.Delete
End If
Next
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 36/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Application.ScreenUpdating= True
Application.DisplayAlerts= True
End Sub
Sub convertToNumbers()
Selection.Value= Selection.Value
rng.Value= WorksheetFunction.Arabic(rng)
End If
Next rng
End Sub
Worksheets("Goal_Seek").Activate
.GoalSeek_ Goal:=Target, _
ChangingCell:=Range("C2")
End With
Exit Sub
End Sub
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 37/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Instead of unhiding rows and columns on by one manually you can use this code to do this in a
single go.
Sub UnhideRowsColumns()
Columns.EntireColumn.Hidden = False
Rows.EntireRow.Hidden = False
End Sub
Sub SaveWorkshetAsPDF()
Dimws As Worksheet
End Sub
Dim i As Integer
If WorksheetFunction.IsError(rng) Then
i = i + 1 rng.Style = "bad"
End If
Next rng
MsgBox "There are total " & i & " error(s) in this worksheet."
End Sub
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 38/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
This code will help you to count the cells which have a speci c value which you will mention
and a er that highlight all those cells.
Sub highlightSpecificValues()
Dim i As Integer
Dim c As Variant
If rng = c Then
rng.Style = "Note"
i = i + 1
End If
Next rng
MsgBox "There are total " & i &" "& c & " in this worksheet."
End Sub
rng.Style = "Note"
End If
Next rng
End Sub
Sub highlightMaxValue()
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 39/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
rng.Style = "Good"
End If
Next rng
End Sub
Sub highlightMinValue()
rng.Style = "Good"
End If
Next rng
End Sub
Sub highlightUniqueValues()
516 Shares = Share w Share Share m Pin
rng.FormatConditions.Delete
Dim uv As UniqueValues
Set uv = rng.FormatConditions.AddUniqueValues
uv.DupeUnique = xlUnique
uv.Interior.Color = vbGreen
End Sub
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 40/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Sub progressStatusBar()
Cells(icntr, 1) = icntr
Application.StatusBar= " Please wait while printing the numbers " & Round((icntr/ 5000 *
End Sub
Sub DisablePageBreaks()
Application.ScreenUpdating= False
Next Sht
Next wb
Application.ScreenUpdating= True
End Sub
516 Shares = Share w Share Share m Pin
Sub columnDifference()
Range("H7:H8,I7:I8").Select
Selection.ColumnDifferences(ActiveCell).Select
Selection.Style= "Bad"
End Sub
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 41/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Sub rowDifference()
Range("H7:H8,I7:I8").Select
Selection.RowDifferences(ActiveCell).Select
Selection.Style= "Bad"
End Sub
Sub printComments()
With ActiveSheet.PageSetup
.printComments= xlPrintSheetEnd
End With
End Sub
Sub printNarrowMargin()
With ActiveSheet.PageSetup
516 Shares = Share w Share Share m Pin
.LeftMargin= Application
.InchesToPoints(0.25)
.RightMargin= Application.InchesToPoints(0.25)
.TopMargin= Application.InchesToPoints(0.75)
.BottomMargin= Application.InchesToPoints(0.75)
.HeaderMargin= Application.InchesToPoints(0.3)
.FooterMargin= Application.InchesToPoints(0.3)
End With
Sub
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 42/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Sub printSelection()
Selection.PrintOutCopies:=1, Collate:=True
End Sub
Sub printCustomSelection()
Exit Sub
End If
Exit Sub
End If
Sub removeNegativeSign()
Selection.Value= Selection.Value
If WorksheetFunction.IsNumber(rng)
End If
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 43/47
7/31/2017 101 Useful Macro Codes Examples For VBA Beginners + [Free PDF Copy]
Next rng
End Sub
Sub replaceBlankWithZero()
Selection.Value= Selection.Value
rng.Value= "0"
Else
End If
Next rng
End Sub
DOWNLOAD NOW
EXCEL PRODUCTIVITY GUIDE
With this guide, you can take your skills to whole next level. It will help you to win at your daily work
using some smart tactics.
http://excelchamps.com/blog/useful-macro-codes-for-vba-newcomers/ 44/47