Download panduan vba macro excel

Bagi sobat yang sedang belajar memabangun aplikasi dengan excel dan inngin menambahkan script vba ke dalamnya, mungkin ebook yang akan kami bagikan pada kesempatan ini dapat membantu anda dalam mengembangkan aplikasi yang anda bangun. Atau bisa sekedar menjadi tambahan refernsi dalam belajar excel dan vba macro. Seperti kita ketahui vba macro sangat membantu kita dalam membangun aplikasi yang sulit atau bahkan tidak bisa dibuat melalui excel saja.

Untuk itu sangat penting bagi anda pecinta atau pembelajar excel, rasanya tidak lengkap ilmu kita tentang excel kalau belum mengetahui tentang menggunakan vba macro dalam excel. Aplikasi yang dibangun dengan excel dan menggunakan vba macro juga terasa lebih mantap dan dinamis.

Silahkan anda download ebook tutorial excel vba macro ini, adapun cakupan pembahasan yang terdapat dalam ebook ini adalah :

1. Mengubah Data Baris Menjadi Kolom Dan Sebaliknya Dengan Transpose

2. Kode VBA Menyembunyikan dan Menampilkan Sheet

3. Mencetak Dokumen Dengan Jumlah Tertentu Menggunakan VBA

4. Menghapus Password Sheet Dengan VBA

5. Macro Berjalan Otomatis Saat Membuka File Excel

6. Kotak Pesan Dengan Pilihan Ya dan Tidak

7. Memberi Warna Background Sel Terpilih

8. Memilih Range Melalui Kotak Pesan

9. Menutup Semua Workbook Yang Tidak Aktif

10. Isi ListBox Sesuai Dengan Data Dalam Sheet

Silahkan anda download ebook tutorial excel vba macro melalui link download di bawah ini :

Demikian yang dapat kami share kepada sobat source code aplikasi pada kesempatan ini, semoga dapat bermanfaat dan bisa menjadi referensi pemrograman bagi anda. Jangan lupa like Fan Page kami, dan SUBSCRIBE Channel Youtube kami untuk dapatkan update source code aplikasi terbaru.

In Excel, macro code is a programming code which is written in VBA (Visual Basic for Applications) language.

The idea behind using a macro code is to automate an action which you perform manually in Excel, otherwise.

For example, you can use a code to print only a particular range of cells just with a single click instead of selecting the range -> File Tab -> Print -> Print Select -> OK Button.

How to use a Macro Code in Excel

Before you use these codes, make sure you have your developer tab on your Excel ribbon to access VB editor. Once you activate developer tab you can use below steps to paste a VBA code into VB editor.

  1. Go to your developer tab and click on "Visual Basic" to open the Visual Basic Editor.
    click-on-visual-basic-editor-before-you-use-these-useful-macros-for-excel
  2. On the left side in "Project Window", right click on the name of your workbook and insert a new module.
    add-module-to-paste-these-useful-macros-for-excel
  3. Just paste your code into the module and close it.
    use-useful-macro-codes-examples-by-pasting-them-into-vb-editor
  4. Now, go to your developer tab and click on the macro button.
    useful-macro-codes-examples-to-use-from-macro-options
  5. It will show you a window with a list of the macros you have in your file from where you can run a macro from that list.
    useful-macro-codes-examples-list-from-macro-option

List of Top 100 macro Examples (CODES) for VBA beginners

I have added all the codes into specific categories so that you can find your favorite codes quickly. Just read the title and click on it to get the code.

note

  • This is my Ultimate VBA Library which I update on monthly basis with new codes and Don't forget to check the VBA Examples Sectionꜜ at the end of this list.
  • VBA is one of the Advanced Excel Skills.
  • To manage all of these codes make sure to read about Personal Macro Workbook to use these codes in all the workbooks.
  • I have tested all of these codes in different versions of Excel (2007, 2010, 2013, 2016, and 2019). If you found any error in any of these codes, make sure to share with me.

Basic Codes

These VBA codes will help you to perform some basic tasks in a flash which you frequently do in your spreadsheets.

1. Add Serial Numbers

Sub AddSerialNumbers() Dim i As Integer On Error GoTo Last i = InputBox("Enter Value", "Enter Serial Numbers") For i = 1 To i ActiveCell.Value = i ActiveCell.Offset(1, 0).Activate Next i Last:Exit Sub End Sub

This macro code will help you to automatically add serial numbers in your Excel sheet which can be helpful for you if you work with large data.

To use this code you need to select the cell from where you want to start the serial numbers and when you run this it shows you a message box where you need to enter the highest number for the serial numbers and click OK. And once you click OK, it simply runs a loop and add a list of serial numbers to the cells downward.

2. Insert Multiple Columns

Sub InsertMultipleColumns() Dim i As Integer Dim j As Integer ActiveCell.EntireColumn.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub

This code helps you to enter multiple columns in a single click. When you run this code it asks you the number columns you want to add and when you click OK, it adds entered number of columns after the selected cell. If you want to add columns before the selected cell, replace the xlToRight to xlToLeft in the code.

3. Insert Multiple Rows

Sub InsertMultipleRows() Dim i As Integer Dim j As Integer ActiveCell.EntireRow.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToDown, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub

With this code, you can enter multiple rows in the worksheet. When you run this code, you can enter the number of rows to insert and make sure to select the cell from where you want to insert the new rows. If you want to add rows before the selected cell, replace the xlToDown to xlToUp in the code.

4. Auto Fit Columns

Sub AutoFitColumns() Cells.Select Cells.EntireColumn.AutoFit End Sub

This code quickly auto fits all the columns in your worksheet. So when you run this code, it will select all the cells in your worksheet and instantly auto-fit all the columns.

5. Auto Fit Rows

Sub AutoFitRows() Cells.Select Cells.EntireRow.AutoFit End Sub

You can use this code to auto-fit all the rows in a worksheet. When you run this code it will select all the cells in your worksheet and instantly auto-fit all the row.

6. Remove Text Wrap

Sub RemoveTextWrap() Range("A1").WrapText = False End Sub

This code will help you to remove text wrap from the entire worksheet with a single click. It will first select all the columns and then remove text wrap and auto fit all the rows and columns. There’s also a shortcut that you can use (Alt + H +‌W) for but if you add this code to Quick Access Toolbar it’s convenient than a keyboard shortcut.

7. Unmerge Cells

Sub UnmergeCells() Selection.UnMerge End Sub

This code simply uses the unmerge options which you have on the HOME‌ tab. The benefit of using this code is you can add it to the QAT and unmerge all the cell in the selection. And if you want to un-merge a specific range you can define that range in the code by replacing the word selection.

8. Open Calculator

Sub OpenCalculator() Application.ActivateMicrosoftApp Index:=0 End Sub

In Windows, there is a specific calculator and by using this macro code you can open that calculator directly from Excel. As I mentioned that it’s for windows and if you run this code in the MAC version of VBA you’ll get an error.

9. Add Header/Footer Date

Sub DateInHeader() With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "&D" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub

This macro adds a date to the header when you run it. It simply uses the tag "&D" for adding the date. You can also change it to the footer or change the side by replacing the "" with the date tag. And if you want to add a specific date instead of the current date you can replace the "&D" tag with that date from the code.

10. Custom Header/Footer

Sub CustomHeader() Dim myText As String myText = InputBox("Enter your text here", "Enter Text") With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = myText .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub

When you run this code, it shows an input box that asks you to enter the text which you want to add as a header, and once you enter it click OK.

If you see this closely you have six different lines of code to choose the place for the header or footer. Let’s say if you want to add left-footer instead of center header simply replace the “myText” to that line of the code by replacing the "" from there.

Formatting Codes

These VBA codes will help you to format cells and ranges using some specific criteria and conditions.

11. Highlight Duplicates from Selection

Sub InsertMultipleColumns() Dim i As Integer Dim j As Integer ActiveCell.EntireColumn.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub0

This macro will check each cell of your selection and highlight the duplicate values.  You can also change the color from the code.

12. Highlight the Active Row and Column

Sub InsertMultipleColumns() Dim i As Integer Dim j As Integer ActiveCell.EntireColumn.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub1

I really love to use this macro code whenever I have to analyze a data table. Here are the quick steps to apply this code.

  1. Open VBE (ALT + F11).
  2. Go to Project Explorer (Ctrl + R, If hidden).
  3. Select your workbook & double click on the name of a particular worksheet in which you want to activate the macro.
  4. Paste the code into it and select the “BeforeDoubleClick” from event drop down menu.
  5. Close VBE and you are done.

Remember that, by applying this macro you will not able to edit the cell by double click.

13. Highlight Top 10 Values

Sub InsertMultipleColumns() Dim i As Integer Dim j As Integer ActiveCell.EntireColumn.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub2

Just select a range and run this macro and it will highlight top 10 values with the green color.

14. Highlight Named Ranges

Sub InsertMultipleColumns() Dim i As Integer Dim j As Integer ActiveCell.EntireColumn.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub3

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.

15. Highlight Greater than Values

Sub InsertMultipleColumns() Dim i As Integer Dim j As Integer ActiveCell.EntireColumn.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub4

Once you run this code it will ask you for the value from which you want to highlight all greater values.

16. Highlight Lower Than Values

Sub InsertMultipleColumns() Dim i As Integer Dim j As Integer ActiveCell.EntireColumn.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub5

Once you run this code it will ask you for the value from which you want to highlight all lower values.

17. Highlight Negative Numbers

Sub InsertMultipleColumns() Dim i As Integer Dim j As Integer ActiveCell.EntireColumn.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub6

Select a range of cells and run this code. It will check each cell from the range and highlight all cells the where you have a negative number.

18. Highlight Specific Text

Sub InsertMultipleColumns() Dim i As Integer Dim j As Integer ActiveCell.EntireColumn.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub7

Suppose you have a large data set and you want to check for a particular value. For this, you can use this code. When you run it, you will get an input box to enter the value to search for.

19. Highlight Cells with Comments

Sub InsertMultipleColumns() Dim i As Integer Dim j As Integer ActiveCell.EntireColumn.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub8

To highlight all the cells with comments use this macro.

20. Highlight Alternate Rows in the Selection

Sub InsertMultipleColumns() Dim i As Integer Dim j As Integer ActiveCell.EntireColumn.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub9

By highlighting alternate rows you can make your data easily readable, and for this, you can use below VBA code. It will simply highlight every alternate row in selected range.

21. Highlight Cells with Misspelled Words

Sub InsertMultipleRows() Dim i As Integer Dim j As Integer ActiveCell.EntireRow.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToDown, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub0

If you find hard to check all the cells for spelling error then this code is for you. It will check each cell from the selection and highlight the cell where is a misspelled word.

22. Highlight Cells With Error in the Entire Worksheet

Sub InsertMultipleRows() Dim i As Integer Dim j As Integer ActiveCell.EntireRow.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToDown, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub1

To highlight and count all the cells in which you have an error, this code will help you. Just run this code and it will return a message with the number error cells and highlight all the cells.

23. Highlight Cells with a Specific Text in Worksheet

Sub InsertMultipleRows() Dim i As Integer Dim j As Integer ActiveCell.EntireRow.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToDown, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub2

This code will help you to count the cells which have a specific value which you will mention and after that highlight all those cells.

24. Highlight all the Blank Cells Invisible Space

Sub InsertMultipleRows() Dim i As Integer Dim j As Integer ActiveCell.EntireRow.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToDown, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub3

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.

25. Highlight Max Value In The Range

Sub InsertMultipleRows() Dim i As Integer Dim j As Integer ActiveCell.EntireRow.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToDown, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub4

It will check all the selected cells and highlight the cell with the maximum value.

26. Highlight Min Value In The Range

Sub InsertMultipleRows() Dim i As Integer Dim j As Integer ActiveCell.EntireRow.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToDown, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub5

It will check all the selected cells and highlight the cell with the Minimum value.

27. Highlight Unique Values

Sub InsertMultipleRows() Dim i As Integer Dim j As Integer ActiveCell.EntireRow.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToDown, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub6

This codes will highlight all the cells from the selection which has a unique value.

28. Highlight Difference in Columns

Sub InsertMultipleRows() Dim i As Integer Dim j As Integer ActiveCell.EntireRow.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToDown, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub7

Using this code you can highlight the difference between two columns (corresponding cells).

29. Highlight Difference in Rows

Sub InsertMultipleRows() Dim i As Integer Dim j As Integer ActiveCell.EntireRow.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToDown, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub8

And by using this code you can highlight difference between two row (corresponding cells).

Printing Codes

These macro codes will help you to automate some printing tasks which can further save you a ton of time. 

30. Print Comments

Sub InsertMultipleRows() Dim i As Integer Dim j As Integer ActiveCell.EntireRow.Select On Error GoTo Last i = InputBox("Enter number of columns to insert", "Insert Columns") For j = 1 To i Selection.Insert Shift:=xlToDown, CopyOrigin:=xlFormatFromRightorAbove Next j Last: Exit Sub End Sub9

Use this macro to activate settings to print cell comments in the end of the page. Let’s say you have 10 pages to print, after using this code you will get all the comments on 11th last page.

31. Print Narrow Margin

Sub AutoFitColumns() Cells.Select Cells.EntireColumn.AutoFit End Sub0

Use this VBA code to take a print with a narrow margin. When you run this macro it will automatically change margins to narrow.

32. Print Selection

Sub AutoFitColumns() Cells.Select Cells.EntireColumn.AutoFit End Sub1

This code will help you print selected range. You don't need to go to printing options and set printing range. Just select a range and run this code.

33. Print Custom Pages

Sub AutoFitColumns() Cells.Select Cells.EntireColumn.AutoFit End Sub2

Instead of using the setting from print options you can use this code to print custom page range. Let’s say you want to print pages from 5 to 10. You just need to run this VBA code and enter start page and end page.

Worksheet Codes

These macro codes will help you to control and manage worksheets in an easy way and save your a lot of time.

34. Hide all but the Active Worksheet

Sub AutoFitColumns() Cells.Select Cells.EntireColumn.AutoFit End Sub3

Now, let's say if you want to hide all the worksheets in your workbook other than the active worksheet. This macro code will do this for you.

Related: VBA Functions List

35. Unhide all Hidden Worksheets

Sub AutoFitColumns() Cells.Select Cells.EntireColumn.AutoFit End Sub4

And if you want to un-hide all the worksheets which you have hide with previous code, here is the code for that.

36. Delete all but the Active Worksheet

Sub AutoFitColumns() Cells.Select Cells.EntireColumn.AutoFit End Sub5

If you want to delete all the worksheets other than the active sheet, this macro is useful for you. When you run this macro it will compare the name of the active worksheet with other worksheets and then delete them.

37. Protect all Worksheets Instantly

Sub AutoFitColumns() Cells.Select Cells.EntireColumn.AutoFit End Sub6

If you want to protect your all worksheets in one go here is a code for you. 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.

38. Resize All Charts in a Worksheet

Sub AutoFitColumns() Cells.Select Cells.EntireColumn.AutoFit End Sub7

Make all chart same in size. This macro code will help you to make all the charts of the same size. You can change the height and width of charts by changing it in macro code.

39. Insert Multiple Worksheets

Sub AutoFitColumns() Cells.Select Cells.EntireColumn.AutoFit End Sub8

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.

40. Protect Worksheet

Sub AutoFitColumns() Cells.Select Cells.EntireColumn.AutoFit End Sub9

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.

41. Un-Protect Worksheet

Sub AutoFitRows() Cells.Select Cells.EntireRow.AutoFit End Sub0

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.

42. Sort Worksheets

Sub AutoFitRows() Cells.Select Cells.EntireRow.AutoFit End Sub1

This code will help you to sort worksheets in your workbook according to their name.

43. Protect all the Cells With Formulas

Sub AutoFitRows() Cells.Select Cells.EntireRow.AutoFit End Sub2

To protect cell with formula with a single click you can use this code.

44. Delete all Blank Worksheets

Sub AutoFitRows() Cells.Select Cells.EntireRow.AutoFit End Sub3

Run this code and it will check all the worksheets in the active workbook and delete if a worksheet is blank.

45. Unhide all Rows and Columns

Sub AutoFitRows() Cells.Select Cells.EntireRow.AutoFit End Sub4

Instead of unhiding rows and columns on by one manually you can use this code to do this in a single go.

46. Save Each Worksheet as a Single PDF

Sub AutoFitRows() Cells.Select Cells.EntireRow.AutoFit End Sub5

This code will simply save all the worksheets in a separate PDF file. You just need to change the folder name from the code.

47. Disable Page Breaks

Sub AutoFitRows() Cells.Select Cells.EntireRow.AutoFit End Sub6

To disable page breaks use this code. It will simply disable page breaks from all the open workbooks.

Workbook Codes

These codes will help you to perform workbook level tasks in an easy way and with minimum efforts. 

48. Create a Backup of a Current Workbook

Sub AutoFitRows() Cells.Select Cells.EntireRow.AutoFit End Sub7

This is one of the most useful macros which can help you to save a backup file of your current workbook.

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.

49. Close all Workbooks at Once

Sub AutoFitRows() Cells.Select Cells.EntireRow.AutoFit End Sub8

Use this macro code to close all open workbooks. 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.

50. Copy Active Worksheet into a New Workbook

Sub AutoFitRows() Cells.Select Cells.EntireRow.AutoFit End Sub9

Let's say if you want to copy your active worksheet in a new workbook, just run this macro code and it will do the same for you. It's a super time saver.

51. Active Workbook in an Email

Sub RemoveTextWrap() Range("A1").WrapText = False End Sub0

Use this macro code to quickly send your active workbook in an e-mail. 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".

52. Add Workbook to a Mail Attachment

Sub RemoveTextWrap() Range("A1").WrapText = False End Sub1

Once you run this macro it will open your default mail client and attached active workbook with it as an attachment.

53. Welcome Message

Sub RemoveTextWrap() Range("A1").WrapText = False End Sub2

You can use auto_open to perform a task on opening a file and all you have to do just name your macro "auto_open".

54. Closing Message

Sub RemoveTextWrap() Range("A1").WrapText = False End Sub3

You can use close_open to perform a task on opening a file and all you have to do just name your macro "close_open".

55. Count Open Unsaved Workbooks

Sub RemoveTextWrap() Range("A1").WrapText = False End Sub4

Let’s you have 5-10 open workbooks, you can use this code to get the number of workbooks which are not saved yet.

Pivot Table Codes

These codes will help you to manage and make some changes in pivot tables in a flash.

56. Hide Pivot Table Subtotals

Sub RemoveTextWrap() Range("A1").WrapText = False End Sub5

If you want to hide all the subtotals, just run this code. First of all, make sure to select a cell from your pivot table and then run this macro.

57. Refresh All Pivot Tables

Sub RemoveTextWrap() Range("A1").WrapText = False End Sub6

A super quick method to refresh all pivot tables. Just run this code and all of your pivot tables in your workbook will be refresh in a single shot.

58. Create a Pivot Table

Follow this step by step guide to create a pivot table using VBA.

59. Auto Update Pivot Table Range

Sub RemoveTextWrap() Range("A1").WrapText = False End Sub7

If you are not using Excel tables then you can use this code to update pivot table range.

60. Disable/Enable Get Pivot Data

Sub RemoveTextWrap() Range("A1").WrapText = False End Sub8

To disable/enable GetPivotData function you need to use Excel option. But with this code you can do it in a single click.

Charts Codes

Use these VBA codes to manage charts in Excel and save your lot of time. 

61. Change Chart Type

Sub RemoveTextWrap() Range("A1").WrapText = False End Sub9

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 different codes for different types, you can find all those types from here.

62. Paste Chart as an Image

Sub UnmergeCells() Selection.UnMerge End Sub0

This code will help you to convert your chart into an image. You just need to select your chart and run this code.

63. Add Chart Title

Sub UnmergeCells() Selection.UnMerge End Sub1

First of all, you need to select your chart and the run this code. You will get an input box to enter chart title.

Advanced Codes

Some of the codes which you can use to preform advanced task in your spreadsheets.

64. Save Selected Range as a PDF

Sub UnmergeCells() Selection.UnMerge End Sub2

If you want to hide all the subtotals, just run this code. First of all, make sure to select a cell from your pivot table and then run this macro.

65. Create a Table of Content

Sub UnmergeCells() Selection.UnMerge End Sub3

Let's say you have more than 100 worksheets in your workbook and it's hard to navigate now.

Don't worry this macro code will rescue everything. When you run this code it will create a new worksheet and create a index of worksheets with a hyperlink to them.

66. Convert Range into an Image

Sub UnmergeCells() Selection.UnMerge End Sub4

Paste selected range as an image. You just have to select the range and once you run this code it will automatically insert a picture for that range.

67. Insert a Linked Picture

Sub UnmergeCells() Selection.UnMerge End Sub5

This VBA code will convert your selected range into a linked picture and you can use that image anywhere you want.

68. Use Text to Speech

Sub UnmergeCells() Selection.UnMerge End Sub6

Just select a range and run this code. Excel will speak all the text what you have in that range, cell by cell.

69. Activate Data Entry Form

Sub UnmergeCells() Selection.UnMerge End Sub7

There is a default data entry form which you can use for data entry.

70. Use Goal Seek

Sub UnmergeCells() Selection.UnMerge End Sub8

Goal Seek can be super helpful for you to solve complex problems. Learn more about goal seek from here before you use this code.

71. VBA Code to Search on Google

Sub UnmergeCells() Selection.UnMerge End Sub9

Formula Codes

These codes will help you to calculate or get results which often you do with worksheet functions and formulas.

72. Convert all Formulas into Values

Sub OpenCalculator() Application.ActivateMicrosoftApp Index:=0 End Sub0

Simply convert formulas into values. When you run this macro it will quickly change the formulas into absolute values.

73. Remove Spaces from Selected Cells

Sub OpenCalculator() Application.ActivateMicrosoftApp Index:=0 End Sub1

One of the most useful macros from this list. It will check your selection and then remove all the extra spaces from that.

74. Remove Characters from a String

Sub OpenCalculator() Application.ActivateMicrosoftApp Index:=0 End Sub2

Simply remove characters from the starting of a text string. 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.

75. Add Insert Degree Symbol in Excel

Sub OpenCalculator() Application.ActivateMicrosoftApp Index:=0 End Sub3

Let’s say you have a list of numbers in a column and you want to add degree symbol with all of them.

76. Reverse Text

Sub OpenCalculator() Application.ActivateMicrosoftApp Index:=0 End Sub4

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.

77. Activate R1C1 Reference Style

Sub OpenCalculator() Application.ActivateMicrosoftApp Index:=0 End Sub5

This macro code will help you to activate R1C1 reference style without using Excel options.

78. Activate A1 Reference Style

Sub OpenCalculator() Application.ActivateMicrosoftApp Index:=0 End Sub6

This macro code will help you to activate A1 reference style without using Excel options.

79. Insert Time Range

Sub OpenCalculator() Application.ActivateMicrosoftApp Index:=0 End Sub7

With this code, you can insert a time range in sequence from 00:00 to 23:00.

80. Convert Date into Day

Sub OpenCalculator() Application.ActivateMicrosoftApp Index:=0 End Sub8

If you have dates in your worksheet and you want to convert all those dates into days then this code is for you. Simply select the range of cells and run this macro.

81. Convert Date into Year

Sub OpenCalculator() Application.ActivateMicrosoftApp Index:=0 End Sub9

This code will convert dates into years.

82. Remove Time from Date

Sub DateInHeader() With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "&D" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub0

If you have time with the date and you want to remove it then you can use this code.

83. Remove Date from Date and Time

Sub DateInHeader() With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "&D" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub1

It will return only time from a date and time value.

84. Convert to Upper Case

Sub DateInHeader() With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "&D" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub2

Select the cells and run this code. It will check each and every cell of selected range and then convert it into upper case text.

85. Convert to Lower Case

Sub DateInHeader() With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "&D" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub3

This code will help you to convert selected text into lower 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.

86. Convert to Proper Case

Sub DateInHeader() With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "&D" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub4

And this code will convert selected text into the proper case where you have the first letter in capital and rest in small.

87. Convert to Sentence Case

Sub DateInHeader() With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "&D" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub5

In text case, you have the first letter of the first word in capital and rest all in words in small for a single sentence and this code will help you convert normal text into sentence case.

88. Remove a Character from Selection

Sub DateInHeader() With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "&D" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub6

To remove a particular character from a selected cell you can use this code. It will show you an input box to enter the character you want to remove.

89. Word Count from Entire Worksheet

Sub DateInHeader() With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "&D" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub7

It can help you to count all the words from a worksheet.

90. Remove the Apostrophe from a Number

Sub DateInHeader() With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "&D" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub8

If you have numeric data where you have an apostrophe before each number, you run this code to remove it.

91. Remove Decimals from Numbers

Sub DateInHeader() With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "&D" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub9

This code will simply help you to remove all the decimals from the numbers from the selected range.

92. Multiply all the Values by a Number

Sub CustomHeader() Dim myText As String myText = InputBox("Enter your text here", "Enter Text") With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = myText .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub0

Let’s you have a list of numbers and you want to multiply all the number with a particular. To use this code: 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.

93. Add a Number in all the Numbers

Sub CustomHeader() Dim myText As String myText = InputBox("Enter your text here", "Enter Text") With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = myText .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub0

Just like multiplying you can also add a number into a set of numbers.

94. Calculate the Square Root

Sub CustomHeader() Dim myText As String myText = InputBox("Enter your text here", "Enter Text") With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = myText .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub2

To calculate square root without applying a formula you can use this code. It will simply check all the selected cells and convert numbers to their square root.

95. Calculate the Cube Root

Sub CustomHeader() Dim myText As String myText = InputBox("Enter your text here", "Enter Text") With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = myText .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub3

To calculate cube root without applying a formula you can use this code. It will simply check all the selected cells and convert numbers to their cube root.

96. Add A-Z Alphabets in a Range

Sub CustomHeader() Dim myText As String myText = InputBox("Enter your text here", "Enter Text") With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = myText .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub4

Sub CustomHeader() Dim myText As String myText = InputBox("Enter your text here", "Enter Text") With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = myText .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub5

Just like serial numbers you can also insert alphabets in your worksheet. Beloware the code which you can use.

97. Convert Roman Numbers into Arabic Numbers

Sub CustomHeader() Dim myText As String myText = InputBox("Enter your text here", "Enter Text") With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = myText .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub6

Sometimes it’s really hard to understand Roman numbers as serial numbers. This code will help you to convert roman numbers into Arabic numbers.

98. Remove Negative Signs

Sub CustomHeader() Dim myText As String myText = InputBox("Enter your text here", "Enter Text") With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = myText .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub7

This code will simply check all the cell in the selection and convert all the negative numbers into positive. Just select a range and run this code.

99. Replace Blank Cells with Zeros

Sub CustomHeader() Dim myText As String myText = InputBox("Enter your text here", "Enter Text") With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = myText .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" End With End Sub8

For data where you have blank cells, you can use the below code to add zeros in all those cells. It makes easier to use those cells in further calculations.

More Codes

100. More VBA Examples and Tutorials

  • User Defined Function [UDF] in Excel using VBA
  • VBA Interview Questions
  • Add a Comment in a VBA Code (Macro)
  • Add a Line Break in a VBA Code (Single Line into Several Lines)
  • Add a New Line (Carriage Return) in a String in VBA
  • Personal Macro Workbook (personal.xlsb)
  • Record a Macro in Excel
  • VBA Exit Sub Statement
  • VBA Immediate Window (Debug.Print)
  • VBA Module
  • VBA MSGBOX
  • VBA Objects
  • VBA With Statement
  • Count Rows using VBA
  • Excel VBA Font (Color, Size, Type, and Bold)
  • Excel VBA Hide and Unhide a Column or a Row
  • Excel VBA Range – Working with Range and Cells in VBA
  • Apply Borders on a Cell using VBA in Excel
  • Find Last Row, Column, and Cell using VBA in Excel
  • Insert a Row using VBA in Excel
  • Merge Cells in Excel using a VBA Code
  • Select a Range/Cell using VBA in Excel
  • How to SELECT ALL the Cells in a Worksheet using a VBA Code
  • use ActiveCell in VBA in Excel
  • How to use Special Cells Method in VBA in Excel
  • How to use UsedRange Property in VBA in Excel
  • VBA AutoFit (Rows, Column, or the Entire Worksheet)
  • VBA ClearContents (from a Cell, Range, or Entire Worksheet)
  • VBA Copy Range to Another Sheet + Workbook
  • VBA Enter Value in a Cell (Set, Get and Change)
  • VBA Insert Column (Single and Multiple)
  • VBA Named Range
  • VBA Range Offset
  • VBA Sort Range | (Descending, Multiple Columns, Sort Orientation
  • VBA Wrap Text (Cell, Range, and Entire Worksheet)
  • How to CLEAR an Entire Sheet using VBA in Excel
  • How to Copy and Move a Sheet in Excel using VBA
  • How to COUNT Sheets using VBA in Excel
  • How to DELETE a SHEET using VBA in Excel
  • How to Hide & Unhide a Sheet using VBA in Excel
  • How to PROTECT and UNPROTECT a Sheet using VBA in Excel
  • RENAME a Sheet using VBA
  • Write a VBA Code to Create a New Sheet
  • VBA Worksheet Object
  • Activate a Sheet using VBA
  • Copy an Excel File (Workbook)
  • VBA Activate Workbook (Excel File)
  • VBA Close Workbook (Excel File)
  • VBA Combine Workbooks (Excel Files)
  • VBA Create New Workbook (Excel File)
  • VBA Delete Workbook (Excel File)
  • VBA Open Workbook (Excel File)
  • VBA Protect/Unprotect Workbook (Excel File)
  • VBA Rename Workbook (Excel File)
  • VBA Save Workbook (Excel File)
  • VBA ThisWorkbook (Current Excel File)
  • VBA Workbook 
  • Declare Global Variable (Public) in VBA
  • Range or a Cell as a Variable in VBA
  • Option Explicit Statement in VBA
  • Variable in a Message Box
  • VBA Constants
  • VBA Dim Statement
  • VBA Variables (Declare, Data Types, and Scope)
  • VBA Add New Value to the Array
  • VBA Array
  • VBA Array Length (Size)
  • VBA Array with Strings
  • VBA Clear Array (Erase)
  • VBA Dynamic Array
  • VBA Loop Through an Array
  • VBA Multi-Dimensional Array
  • VBA Range to an Array
  • VBA Search for a Value in an Array
  • VBA Sort Array
  • How to Average Values in Excel using VBA
  • Get Today’s Date and Current Time using VBA
  • Sum Values in Excel using VBA
  • Match Function in VBA
  • MOD in VBA
  • Random Number
  • VBA Calculate (Cell, Range, Row, & Workbook)
  • VBA Concatenate
  • VBA Worksheet Function (Use Excel Functions in a Macro)
  • How to Check IF a Sheet Exists using VBA in Excel
  • VBA Check IF a Cell is Empty + Multiple Cells
  • VBA Check IF a Workbook Exists in a Folder (Excel File)
  • VBA Check IF a Workbook is Open (Excel File)
  • VBA Exit IF
  • VBA IF – IF Then Else Statement
  • VBA IF And (Test Multiple Conditions)
  • VBA IF Not
  • VBA IF OR (Test Multiple Conditions)
  • VBA Nested IF
  • VBA SELECT CASE Statement (Test Multiple Conditions)
  • VBA Automation Error (Error 440)
  • VBA Error 400
  • VBA ERROR Handling
  • VBA Invalid Procedure Call Or Argument Error (Error 5)
  • VBA Object Doesn’t Support this Property or Method Error (Error 438)
  • VBA Object Required Error (Error 424)
  • VBA Out of Memory Error (Error 7)
  • VBA Overflow Error (Error 6)
  • VBA Runtime Error (Error 1004)
  • VBA Subscript Out of Range Runtime Error (Error 9)
  • VBA Type Mismatch Error (Error 13)
  • Excel VBA Do While Loop and (Do Loop While)
  • How to Loop Through All the Sheets using VBA
  • Loop Through a Range using VBA
  • VBA FOR LOOP
  • VBA GoTo Statement
  • Input Box in VBA
  • VBA Create and Write to a Text File
  • VBA ScreenUpdating
  • VBA Status Bar
  • VBA Wait and Sleep

Share0

Tweet0

About the Author

puneet one point one

Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can find him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.