Excel Macro Basic
Excel Macro Basic
codes. It would be great if you bookmark this page and keep on visiting to new
codes every time.
Once you run this macro it will show you an input box where you need to enter max
number for the serial numbers and after that, it will insert numbers in the column
in a sequence.
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
Last:Exit Sub
End Sub
This macro code will select all the cells in your worksheet and instantly auto-fit
all the columns.
When you run this code it will select all the cells in your worksheet and instantly
auto-fit all the row.
It will first select all the columns and then remove text wrap and auto fit all the
rows and columns.
7. Unmerge Cells
Select your cells and run this code and it will un-merge all the cells from the
selection with your loosing data.
8. Open Calculator
In window there is a specific calculator and by using this macro code you can open
that calculator directly from Excel use for your calculations.
You can edit this code for switching from header to footer.
Sub
TopTen()Selection.FormatConditions.AddTop10Selection.FormatConditions(Selection.For
matConditions.Count).SetFirstPriorityWith Selection.FormatConditions(1).TopBottom =
xlTop10Top.Rank = 10.Percent = FalseEnd WithWith
Selection.FormatConditions(1).Font.Color = -16752384.TintAndShade = 0End WithWith
Selection.FormatConditions(1).Interior.PatternColorIndex = xlAutomatic.Color =
13561798.TintAndShade = 0End WithSelection.FormatConditions(1).StopIfTrue =
FalseEnd Sub
14. Highlight Named Ranges
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.
When you run it, you will get an input box to enter the value to search for.
Sub
highlightCommentCells()Selection.SpecialCells(xlCellTypeComments).SelectSelection.S
tyle= �Note�End Sub
It will check each cell from the selection and highlight the cell where is a
misspelled word.
Just run this code and it will return a message with the number error cells and
highlight all the cells.
24. Highlight all the Cells in a Worksheet which are Blank but have an Invisible
Space
Sometimes there are some cells which are blank but they have a single space and due
to this, it�s really hard to identify them.
This code will check all the cell in the worksheet and highlight all the cells
which have a single space.
Sub
columnDifference()Range(�H7:H8,I7:I8�).SelectSelection.ColumnDifferences(ActiveCell
).SelectSelection.Style= �Bad�End Sub
Sub
rowDifference()Range(�H7:H8,I7:I8�).SelectSelection.RowDifferences(ActiveCell).Sele
ctSelection.Style= �Bad�End Sub
Let�s say you have 10 pages to print, after using this code you will get all the
comments on 11th last page.
Sub printComments()
With ActiveSheet.PageSetup
.printComments= xlPrintSheetEnd
End With
End Sub
When you run this macro it will automatically change margins to narrow.
End Sub
Sub printSelection()
Selection.PrintOutCopies:=1, Collate:=True
End Sub
You just need to run this VBA code and enter start page and end page.
When you run this macro it will compare the name of the active worksheet with other
worksheets and then delete them.
When you run this macro, you will get an input box to enter a password. Once you
enter your password, click OK.
You can change the height and width of charts by changing it in macro code.
When you run this macro code you will get an input box to enter the total number of
sheets you want to enter.
All you have to do just mention your password which you have used while protecting
your worksheet.
It will save a backup file in the same directory where your current file is saved
and it will also add the current date with the name of the file.
This macro code will first check all the workbooks one by one and close them. If
any of the worksheets is not saved, you�ll get a message to save it.
Sub CopyWorksheetToNewWorkbook()ThisWorkbook.ActiveSheet.Copy
_Before:=Workbooks.Add.Worksheets(1)End Sub
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�.
Sub auto_close()MsgBox �Bye Bye! Don�t forget to check other cool stuff on
excelchamps.com�End Sub
First of all, make sure to select a cell from your pivot table and then run this
macro.
Just run this code and all of your pivot tables in your workbook will be refresh in
a single shot.
Sub CopyWorksheetToNewWorkbook()ThisWorkbook.ActiveSheet.Copy
_Before:=Workbooks.Add.Worksheets(1)End Sub
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 different codes for different types, you can find all those types from
here.
You just need to select your chart and run this code.
Sub
ConvertChartToPicture()ActiveChart.ChartArea.CopyActiveSheet.Range(�A1�).SelectActi
veSheet.Pictures.Paste.SelectEnd Sub
First of all, make sure to select a cell from your pivot table and then run this
macro.
You just have to select the range and once you run this code it will automatically
insert a picture for that range.
Sub PasteAsPicture()Application.CutCopyMode =
FalseSelection.CopyActiveSheet.Pictures.Paste.SelectEnd Sub
Sub LinkedPicture()Selection.CopyActiveSheet.Pictures.Paste(Link:=True).SelectEnd
Sub
Excel will speak all the text what you have in that range, cell by cell.
Learn more about goal seek from here before you use this code.
search_string = query
�Uncomment the following line for Windows 64 versions and comment out Windows 32
versions�
chromePath = �C:Program
FilesGoogleChromeApplicationchrome.exe�
�Uncomment the following line for Windows 32 versions and comment out Windows 64
versions
(x86)GoogleChromeApplicationchrome.exe�
End Sub
When you run this macro it will quickly change the formulas into absolute values.
It will check your selection and then remove all the extra spaces from that.
All you need is to refer to a cell or insert a text into the function and number of
characters to remove from the text string.
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 first characters from a cell, you need to enter
1 in cnt.
It will check each and every cell of selected range and then convert it into upper
case text.
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.
It will show you an input box to enter the character you want to remove.
Select that range of cells and run this code. It will first ask you for the number
with whom you want to multiple and then instantly multiply all the numbers with it.
It will simply check all the selected cells and convert numbers to their square
root.
It will simply check all the selected cells and convert numbers to their cube root.
It makes easier to use formula and use those cells in further calculations.
100?
Well, I want you to share your favorite macro code which you use everyday to save
your time.
Please share with me in the comment section, I�d love to hear from you.
And in the end, I just want to say that some these codes every day to increase my
productivity and I�m sure it will also help you in your work.
I hope you have found this list useful. If yes, then please don�t forget to share
this list with your friends.