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. Show 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 ExcelBefore 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.
List of Top 100 macro Examples (CODES) for VBA beginnersI 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.
Basic CodesThese VBA codes will help you to perform some basic tasks in a flash which you frequently do in your spreadsheets. 1. Add Serial NumbersSub 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 ColumnsSub 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 RowsSub 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 ColumnsSub 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 RowsSub 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 WrapSub 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 CellsSub 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 CalculatorSub 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 DateSub 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/FooterSub 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 CodesThese VBA codes will help you to format cells and ranges using some specific criteria and conditions. 11. Highlight Duplicates from SelectionSub 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 ColumnSub 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.
Remember that, by applying this macro you will not able to edit the cell by double click. 13. Highlight Top 10 ValuesSub 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 RangesSub 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 ValuesSub 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 ValuesSub 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 NumbersSub 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 TextSub 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 CommentsSub 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 SelectionSub 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 WordsSub 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 WorksheetSub 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 WorksheetSub 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 SpaceSub 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 RangeSub 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 RangeSub 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 ValuesSub 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 ColumnsSub 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 RowsSub 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 CodesThese macro codes will help you to automate some printing tasks which can further save you a ton of time. 30. Print CommentsSub 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 MarginSub 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 SelectionSub 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 PagesSub 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 CodesThese 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 WorksheetSub 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 WorksheetsSub 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 WorksheetSub 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 InstantlySub 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 WorksheetSub 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 WorksheetsSub 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 WorksheetSub 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 WorksheetSub 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 WorksheetsSub 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 FormulasSub 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 WorksheetsSub 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 ColumnsSub 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 PDFSub 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 BreaksSub 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 CodesThese codes will help you to perform workbook level tasks in an easy way and with minimum efforts. 48. Create a Backup of a Current WorkbookSub 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 OnceSub 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 WorkbookSub 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 EmailSub 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 AttachmentSub 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 MessageSub 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 MessageSub 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 WorkbooksSub 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 CodesThese codes will help you to manage and make some changes in pivot tables in a flash. 56. Hide Pivot Table SubtotalsSub 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 TablesSub 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 TableFollow this step by step guide to create a pivot table using VBA. 59. Auto Update Pivot Table RangeSub 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 DataSub 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 CodesUse these VBA codes to manage charts in Excel and save your lot of time. 61. Change Chart TypeSub 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. 62. Paste Chart as an ImageSub 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 TitleSub 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 CodesSome of the codes which you can use to preform advanced task in your spreadsheets. 64. Save Selected Range as a PDFSub 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 ContentSub 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 ImageSub 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 PictureSub 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 SpeechSub 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 FormSub UnmergeCells() Selection.UnMerge End Sub7 There is a default data entry form which you can use for data entry. 70. Use Goal SeekSub 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 GoogleSub UnmergeCells() Selection.UnMerge End Sub9 Formula CodesThese codes will help you to calculate or get results which often you do with worksheet functions and formulas. 72. Convert all Formulas into ValuesSub 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 CellsSub 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 StringSub 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 ExcelSub 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 TextSub 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 StyleSub 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 StyleSub 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 RangeSub 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 DaySub 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 YearSub OpenCalculator() Application.ActivateMicrosoftApp Index:=0 End Sub9 This code will convert dates into years. 82. Remove Time from DateSub 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 TimeSub 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 CaseSub 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 CaseSub 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 CaseSub 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 CaseSub 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 SelectionSub 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 WorksheetSub 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 NumberSub 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 NumbersSub 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 NumberSub 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 NumbersSub 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 RootSub 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 RootSub 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 RangeSub 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 NumbersSub 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 SignsSub 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 ZerosSub 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 Codes100. More VBA Examples and Tutorials
Share0 Tweet0 About the Author 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. |