40 Advanced Useful VBA Codes for Excel [Free PDF Download]
Excel is one of the most widely used programs in the world, and it’s no secret that its VBA programming language is a powerful tool for automating tasks and increasing productivity. With advanced VBA codes, you can take your Excel skills to the next level and streamline your workflows even further. That’s why I’ve compiled a list of 40 advanced useful VBA codes for Excel that you can download for free in PDF format.
So without further ado, let’s dive into the world of advanced VBA codes for Excel and see what’s in store.
Download 40 Advanced Useful VBA Codes for Excel

5 Advanced Useful VBA Codes to Export Files in Excel
1. Export Each Worksheet in a Workbook as Separate Excel Files Using VBA
This code will allow you to export all the sheets in your workbook as separate Excel files. You will get a prompt window to choose a location to save all the Excel files.
2. Export All Worksheets in a Workbook as Separate PDF Files Using VBA
This code will allow you to export all the sheets in your workbook as separate PDF files. You will get a prompt window to choose a location to save all the pdf files.
3. VBA Code to Export Worksheet as a PDF File with a Prompt [Current Date & Time in the Filename]
This code can export a worksheet as a pdf file. The file name will start with the sheet name followed by the current date & time. You will get a prompt to choose a specific location to save the pdf file. Also, you will be allowed to edit the file name while saving it.
4. Export Charts from Excel to PowerPoint Using VBA
This code exports a selected chart in Excel to a new PowerPoint slide. It first checks if a chart is selected and displays a message if one isn’t. If a chart is selected, it adds a slide with a title-only layout, copies the selected chart, and pastes it onto the PowerPoint slide.
5. Select and Export Range as PDF in Excel with VBA
This code allows you to select and insert a range and then export the range as a PDF file. You will also get a prompt to manually choose a destination to save and rename the file.
4 Advanced Useful VBA Codes for Range Manipulation in Excel
1. Select a Range to Apply Alternate Row Colors in Excel Using VBA
This VBA code allows you to select a range and apply alternate row colors to that range.
2. Remove Blank Rows in the Active Worksheet in Excel Using VBA
This code can remove all the blank rows inside your data in Excel when you run it.
3. Unhide All Rows and Columns in the Active Worksheet in Excel Using VBA
This code will let you unhide all the hidden rows and columns in one go.
4. Unmerge All Merged Cells in Excel Using VBA
This VBA code will unmerge all the merged cells in your active worksheet in Excel.
4 Advanced Useful VBA Codes for Sheet Manipulation in Excel
1. Delete Multiple Sheets Without Any Warning Prompt in Excel Using VBA
This subroutine deletes multiple sheets without any warning prompt with the given names. Just run the code, insert sheet names to delete, separate by commas, and see the magic.
2. Unhide All Worksheets in Your Excel Workbook Using VBA
This code allows you to unhide all the worksheets at the same time.
3. Sort Worksheets Alphabetically in Excel Using VBA
This VBA code sorts the worksheets in an Excel workbook alphabetically based on your input. It asks you to choose whether you want to sort the worksheets in ascending order (A-Z), descending order (Z-A), or exit the sorting process.
4. Check Whether a Specific Sheet Exists in a Workbook Using VBA
This VBA code helps you check whether a sheet with a specific name exists in your Excel workbook or not. If it finds a match, it displays a message box telling you that the sheet exists and exits the sub. If it doesn’t find a match, it displays another message box informing you that the sheet does not exist.
5 Advanced Useful VBA Codes for Workbook Manipulation in Excel
1. Combine Multiple Excel Workbooks into a Single Workbook Using VBA
The following sub-routine combines multiple Excel workbooks into a single workbook. It prompts the user to select multiple files using the File Dialog Box. It then opens each file, copies all worksheets, and pastes them into the destination workbook.
2. Delete All Blank Worksheets from an Excel Workbook Using VBA
This VBA code loops through all the worksheets in the active workbook and checks if each worksheet is blank. If a worksheet is blank, it is deleted without any confirmation message.
3. Refresh All Pivot Tables in the Active Workbook Using VBA
This code loops through all pivot tables in the active workbook using a For Each loop and then refreshes each pivot table using the RefreshTable method.
4. Activate R1C1 Reference Style in Excel Using VBA
This code sets the reference style of Excel from the A1 reference style to the R1C1 reference style.
5. Activate A1 Reference Style in Excel Using VBA
This code sets the reference style of Excel from the R1C1 reference style to the A1 reference style.
4 Advanced Useful VBA Codes for Data Manipulation in Excel
1. Create a List of All Sheets [Table of Contents] in Excel Using VBA
This VBA code creates a sheet named “Table of Contents” that lists all the other worksheets in the workbook, excluding the “Table of Contents” sheet itself. Then the code loops through all the worksheets in the workbook, excluding the “Table of Contents” sheet, and adds their names by inserting a hyperlink to each sheet.
2. Transfer Data from Excel to Powerpoint Using VBA
This VBA code asks you to select a range of cells in your Excel sheet. Then it opens the PowerPoint application, adds a new presentation and slide to it, and pastes the selected range as a table onto the new PowerPoint slide.
3. Remove All Extra Spaces from a Selected Range in Excel with VBA
This code allows you to manually select a range of cells in an Excel workbook using a prompt. Then it removes any leading and trailing spaces from the cell values, as well as any extra spaces between words.
4. Search Value on Multiple Sheets in Excel with VBA
This code allows you to search for a specific value on all the worksheets in the current workbook. It will prompt you to enter the search term and then loop through each worksheet in the workbook to find the first occurrence of the value.
If it finds the value, it will display a message box showing the worksheet name and cell address where the value is found. If it does not find the value on any sheet, it displays a message indicating that the value was not found.
7 Advanced Useful VBA Codes for Formatting in Excel
1. AutoFit All Non-Blank Columns in the Active Worksheet in Excel Using VBA
This VBA code will autofit all non-blank columns in the active worksheet of an Excel workbook.
2. AutoFit All Non-Blank Rows in the Active Worksheet in Excel Using VBA
This VBA code will autofit all non-blank rows in the active worksheet of an Excel workbook.
3. Highlight All the Cells Having Formulas in Excel with VBA
This VBA code loops through all the cells in the used range of the active sheet and checks if each cell contains a formula. If a cell contains a formula (i.e., if the first character of the cell is “=”), then it changes the cell color to yellow.
4. Change Letter Case in Excel Using VBA
This subroutine changes the letter case of the selected cells based on user input. To change the case, you have to select the cells first and then run the code. It will work like magic.
5. Highlight Cells with Wrongly Spelled Words in Excel Using VBA
This code highlights the cells that have misspelled words in the active worksheet.
6. Change the Font Size of All Sheets of an Entire Workbook with VBA
This code prompts you to enter a font size, and loops through all worksheets in the workbook. Then it changes the font size of all cells in each sheet to the entered font size. If you cancel or enter an invalid input (e.g. a negative number), the macro exits without making any changes.
7. VBA to Remove All Text Wraps in the Active Worksheet
This code removes all the text wraps in your active worksheet in Excel.
5 Advanced Useful VBA Codes to Print Files in Excel
1. VBA to Select and Print Multiple Ranges On Separate Pages in Excel
This piece of code allows you to specify the number of ranges first and then input those ranges by selecting cells using a prompt window. After that, it will tell you to save the ranges as separate PDF files to start printing each range on separate pages.
2. VBA to Print Selected Sheets Using Sheet Numbers in Excel
This code will allow you to print a number of selected sheets. After running the code, you will be given the option to insert the starting and ending sheet numbers on your workbook. Based on your given sheet numbers, it will save and print those sheets one by one. Keep in mind that, this code only works on consecutive sheets.
3. VBA to Print Selected Sheets in Excel By Mentioning the Sheet Names
By using this code, you can print a number of selected sheets by mentioning the sheet names on the code.
4. VBA to Print the Active Worksheet with Comments in Excel
This code will allow you to print out your active worksheet with all the comments in it.
5. VBA to Print All the Hidden As Well As Visible Worksheets in Excel
This code can print out all the hidden as well as visible worksheets on your workbook. It will allow you to save all the sheets one by one first and then print them out respectively.
6 Miscellaneous VBA Codes for Excel
1. Select All Non-Blanks Cells in the Active Worksheet Using Excel VBA
This code selects all the cells with data in the active worksheet.
2. VBA to Remove Page Breaks from the Active Worksheet in Excel
This code lets you remove page breaks from the current worksheet in just one click.
3. VBA to Count the Total Number of Non-Blank Rows in a Selected Range in Excel
This subroutine counts the number of non-blank rows in the selected range. Just select a range and then run the code. It will show the count of all non-blank rows in a pop-up dialog box.
4. VBA to Count the Total Number of Non-Blank Columns in the Active Worksheet in Excel
This code can count the total number of non-blank columns in the active worksheet in Excel. Just run the code and you will get the count figure in a popped-up dialog box.
5. VBA to Read Contents of a Selected Range Using Text-To-Speech in Excel
This code will prompt you to select a range. Next, it will read the contents of each cell using text-to-speech.
6. VBA to Search on Google from Your Excel Worksheet
This code will prompt you to enter a search query in an input box. Upon entering the query and clicking OK, the code replaces any spaces in the query with a + sign and creates a Google search URL by appending the q parameter (which represents the search query) to the base URL https://www.google.com/search?q=. Finally, it opens the constructed URL in your default web browser.
Conclusion
I hope that this collection of 40 advanced useful VBA codes for Excel has been helpful to you and that you’re able to apply them in your daily work. Remember, these codes are just a starting point, and there’s always more to learn. Keep exploring the world of VBA and see how you can further customize Excel to suit your needs.