40 pages
Ready-to-use VBA macros for Excel
Copy-paste VBA macros for everyday Excel jobs, each explained line by line with how to install it, how to change it and what can go wrong.
Sheets
- VBA macro to create a table of contents with hyperlinks in ExcelBuild a Contents sheet that lists every sheet with a clickable hyperlink. Run it again any time to refresh the list after adding or renaming sheets.
- VBA macro to create sheets from a list of names in ExcelSelect a list of names and create one new worksheet for each, with invalid and duplicate names reported instead of crashing the macro.
- VBA macro to hide all sheets except the active one in ExcelHide every sheet except the one you are looking at with one VBA macro, with an option to make them very hidden so the Unhide menu cannot show them.
- VBA macro to list all sheet names in ExcelWrite the name, position, type and visibility of every sheet in a workbook to a new sheet with a short VBA macro. Hidden sheets are included.
- VBA macro to protect or unprotect all sheets at once in ExcelTwo VBA macros that protect or unprotect every worksheet in a workbook with one password prompt, and report any sheet that could not be unlocked.
- VBA macro to rename sheets from a list in ExcelRename many sheets at once from a two-column list of old and new names. The macro skips missing sheets and reports invalid names instead of stopping.
- VBA macro to sort sheets alphabetically in ExcelPut the sheet tabs of a workbook in A to Z (or Z to A) order with one VBA macro. Works with any number of sheets, including chart sheets.
- VBA macro to unhide all sheets in ExcelA ready-to-use VBA macro that unhides every hidden and very hidden sheet in a workbook in one go, with a check for workbook protection.
Workbooks and files
- VBA macro to combine all sheets into one sheet in ExcelStack the data from every worksheet of a workbook into one Combined sheet, with the sheet name in the first column. Headings are taken once.
- VBA macro to create a folder for each name in a listSelect a list of names in Excel and create one folder for each in a folder you choose. Existing folders are skipped and invalid characters replaced.
- VBA macro to import a CSV file into ExcelPick a CSV file and import it into a new sheet with the right delimiter, UTF-8 characters and no broken quoted fields. Options for leading zeros and semicolons.
- VBA macro to list all files in a folder in ExcelWrite the name, size, date and a clickable link for every file in a folder to a new workbook. Includes a version that also lists subfolders.
- VBA macro to merge all workbooks in a folder into one sheetCombine the first sheet of every Excel file in a folder into one sheet, stacked below each other with a column showing which file each row came from.
- VBA macro to save a backup copy with a timestamp in ExcelSave a dated copy of the active workbook in a Backups folder with one click, without changing the file you are working on.
- VBA macro to save each sheet as a separate workbook in ExcelSave every visible sheet of a workbook as its own .xlsx file in the same folder, named after the sheet. Optional: values only, no links.
- VBA macro to split a sheet into workbooks by column valueSplit one data sheet into a separate workbook for every region, department or customer in a column, saved next to the original file.
Data
- VBA macro to change text case (UPPER, lower, Proper) in ExcelChange the selected text to UPPER CASE, lower case or Proper Case in place, without helper columns. Formulas and numbers are left alone.
- VBA macro to convert formulas to values in ExcelReplace the formulas in the selected cells with their current results, keeping number formats. Includes a whole-workbook version and a safety prompt.
- VBA macro to delete blank rows in ExcelDelete every completely empty row on the active sheet in one go. The macro collects the rows first and deletes them in a single fast step.
- VBA macro to delete rows based on a cell value in ExcelDelete every row whose value in a chosen column matches a word such as Cancelled. Fast Union-based deletion, case-insensitive matching, easy to adapt.
- VBA macro to find and replace across all sheets in ExcelFind and replace text in every worksheet of a workbook at once, including protected-sheet checks, wildcard notes and a match-whole-cell option.
- VBA macro to insert multiple rows or columns in ExcelInsert any number of blank rows above the active cell, or columns to its left, and insert a blank row between every row of data.
- VBA macro to refresh all pivot tables in ExcelRefresh every pivot table on every sheet of a workbook with one macro, without refreshing queries and connections, and count how many were updated.
- VBA macro to remove duplicates based on one column in ExcelRemove duplicate rows based on the values in one column with RemoveDuplicates in VBA, and report how many rows were removed.
- VBA macro to trim spaces in selected cells in ExcelRemove leading, trailing and double spaces (including non-breaking spaces from web pages) from the text in the selected cells, leaving formulas alone.
Formatting
- VBA function to count cells by background color in ExcelA user-defined function, CountByColor, that counts the cells in a range whose fill colour matches a sample cell. Use it in any formula.
- VBA function to sum cells by background color in ExcelA user-defined function, SumByColor, that adds up the numbers in cells whose fill colour matches a sample cell. Text and empty cells are ignored.
- VBA macro to auto-fit all columns on all sheets in ExcelAuto-fit the column widths on every sheet of a workbook at once, with an optional maximum width so long text does not create huge columns.
- VBA macro to highlight duplicate values in ExcelColour every cell in the selection whose value appears more than once. Works on Windows and Mac, plus a one-line conditional formatting alternative.
- VBA macro to remove all hyperlinks in ExcelRemove every hyperlink from all sheets of a workbook, or only from the selection, while keeping the cell text. Includes a count and a confirmation.
- VBA macro to unmerge cells and fill down the values in ExcelUnmerge every merged cell in the selection and fill the value into all cells of the former merged area, so the data can be sorted, filtered and pivoted.
Printing and PDF
- VBA macro to export a range as PDF in ExcelSave the selected range (an invoice, a summary block, a chart area) as a PDF with a date in the file name, then open it.
- VBA macro to save each sheet as a separate PDF in ExcelExport every visible, non-empty sheet of a workbook to its own PDF file named after the sheet, or the whole workbook to one PDF.
Email and other Office apps
- VBA macro to send a range in the body of an Outlook emailPut the selected Excel range into the body of an Outlook email as a formatted table, above your normal signature. Windows and classic Outlook.
- VBA macro to send the active workbook by Outlook emailAttach the active workbook, including unsaved changes, to a new Outlook email with recipient, subject and text filled in. Display first or send directly.
Utilities
- VBA function to extract the URL from a hyperlink in ExcelA user-defined function, LinkAddress, that returns the web address behind a hyperlinked cell, plus a macro that writes every address next to its link.
- VBA macro to add a timestamp when a cell changes in ExcelWrite the date and time next to a cell automatically whenever someone edits it, using the Worksheet_Change event. Static, not recalculated like NOW().
- VBA macro to reset the used range and clear excess formattingShrink a bloated used range on every sheet by deleting empty rows and columns after the last real data, then make Excel recalculate the used range.
- VBA macro to set all sheets to 100% zoom and select A1Tidy a workbook before sending it: every sheet at 100% zoom, scrolled to the top with A1 selected, and the first sheet active. One macro, any workbook.
- VBA macro to show a progress bar in the Excel status barShow a text progress bar with percentage in Excel's status bar while a long macro runs, and restore the status bar safely afterwards, even after errors.