Category: VBA

Automation scripts, macros, and custom functions to speed up daily office workflows.

  • How to Merge Multiple Sheets into One Master Sheet Using Excel VBA

    If you regularly manage workbooks with dozens of daily or monthly tabs, manually copying and pasting rows into a master summary sheet is tedious and prone to human error.

    In this guide, you’ll learn how to run a simple VBA macro that automatically combines data from all open worksheets into a single, clean Master Sheet with just one click.


    Why Automate Sheet Consolidation?

    Consolidating data across multiple tabs is a daily task in reporting, inventory management, and financial audits. Using VBA for this workflow ensures:

    • Zero Manual Mistakes: Prevents missed rows or double-pasted datasets.
    • Dynamic Row Detection: Works automatically regardless of how many rows each sheet contains.
    • Header Protection: Copies column headers only once from the first sheet.

    Step 1: Open the VBA Editor Window

    1. Open your Excel workbook containing the sheets you wish to combine.
    2. Press Alt + F11 (Option + F11 on Mac) to launch the Visual Basic Editor.
    3. In the top menu bar, click Insert ➔ Module to open a clean code window.

    Step 2: Copy and Paste the Consolidation VBA Code

    Copy the code block below and paste it directly into your blank module window:

    Sub CombineAllSheets()
        Dim ws As Worksheet
        Dim masterWs As Worksheet
        Dim lastRow As Long
        Dim masterLastRow As Long
        Dim isFirstSheet As Boolean
        
        isFirstSheet = True
        
        ' 1. Disable screen updating for faster execution
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        
        ' 2. Delete existing "Master" sheet if it already exists
        On Error Resume Next
        Worksheets("Master").Delete
        On Error GoTo 0
        
        ' 3. Create a new "Master" sheet at the beginning
        Set masterWs = Worksheets.Add(Before:=Worksheets(1))
        masterWs.Name = "Master"
        
        ' 4. Loop through every sheet in the workbook
        For Each ws In Worksheets
            If ws.Name <> masterWs.Name Then
                lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
                
                ' Ensure sheet has data beyond row 1
                If lastRow >= 1 Then
                    If isFirstSheet Then
                        ' Copy header + data from the first worksheet
                        ws.Rows("1:" & lastRow).Copy masterWs.Range("A1")
                        isFirstSheet = False
                    Else
                        ' Find next empty row in Master sheet
                        masterLastRow = masterWs.Cells(masterWs.Rows.Count, "A").End(xlUp).Row + 1
                        ' Copy data only (skip header row 1)
                        ws.Rows("2:" & lastRow).Copy masterWs.Range("A" & masterLastRow)
                    End If
                End If
            End If
        Next ws
        
        ' 5. Auto-fit column widths on Master Sheet
        masterWs.Columns.AutoFit
        
        ' 6. Re-enable updates & show alert
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
        
        MsgBox "All sheets successfully merged into 'Master' sheet!", vbInformation, "Excel Owl Automation"
    End Sub

    Step 3: Run the Macro Using Alt + F8

    You can execute this automation right from your Excel window at any time:

    1. Switch back to your main Excel window.
    2. Press Alt + F8 (or Option + F8 on Mac) to bring up the Macro dialog box.
    3. Select CombineAllSheets from the list.
    4. Click Run.

    💡 Pro Tip: Quick Execution inside VBA Editor
    If you are actively working in the code editor, place your cursor anywhere inside the script and press F5 to run it immediately.


    Step 4: Save as Macro-Enabled Workbook (.xlsm)

    To keep your macro active for future use, save your file in the macro-enabled format:

    1. Click File ➔ Save As (or press F12).
    2. In the Save as type drop-down menu, choose Excel Macro-Enabled Workbook (*.xlsm).
    3. Click Save.

    ⚠️ Important Note: Regular .xlsx files do not support macro scripts. Saving as .xlsx will erase your VBA code permanently.

  • How to Automate Data Clean-Up in Excel Using VBA (Beginner Guide)

    If you find yourself manually deleting blank rows, trimming trailing spaces, and formatting headers every single day, Excel VBA (Visual Basic for Applications) can save you hours of repetitive work.

    In this guide, you will learn how to set up VBA for the first time and run a simple macro that automates core data clean-up tasks with just one click.


    What is an Excel Macro (VBA)?

    A Macro is a series of recorded commands or automated scripts written in VBA (Visual Basic for Applications). Think of it as a personal assistant inside Excel that executes repetitive tasks instantly without human error.


    Step 1: Open the VBA Editor Window

    1. Open your Excel workbook.
    2. Press Alt + F11 (Option + F11 on Mac) on your keyboard to open the Visual Basic Editor.
    3. In the top menu bar of the editor, click Insert ➔ Module. A blank white code window will appear.

    Step 2: Copy and Paste the VBA Code

    Copy the ENTIRE code block below (from Sub down to End Sub) and paste it directly into the blank module window:

    Sub AutomateDataCleanup()
        Dim ws As Worksheet
        Set ws = ActiveSheet
        
        ' 1. Pause screen updating to speed up code execution
        Application.ScreenUpdating = False
        
        ' 2. Trim excess spaces from all used cells
        Dim cell As Range
        For Each cell In ws.UsedRange
            If Not IsEmpty(cell.Value) Then
                cell.Value = Trim(cell.Value)
            End If
        Next cell
        
        ' 3. Auto-fit all column widths
        ws.UsedRange.Columns.AutoFit
        
        ' 4. Re-enable screen updating
        Application.ScreenUpdating = True
        
        ' 5. Completion notification
        MsgBox "Data Clean-Up Completed!", vbInformation, "Excel Owl Automation"
    End Sub

    Step 3: Run the Macro Using Alt + F8 Shortcut

    You don’t need to open the VBA Editor every time you want to clean up your data. You can run your macro directly from your Excel spreadsheet using a quick shortcut:

    1. Switch back to your main Excel sheet window.
    2. Press Alt + F8 (Option + F8 on Mac) to open the Macro Dialog Box.
    3. Select AutomateDataCleanup from the list of available macros.
    4. Click the Run button on the right.

    💡 Pro Tip: Running directly inside VBA Editor
    If you are still inside the VBA Editor code window, simply place your cursor anywhere inside the code and press F5 (or click the green Run ▶ icon at the top toolbar) to execute it immediately.


    Step 4: Save as Macro-Enabled Workbook (.xlsm)

    To ensure your new macro is preserved when you close Excel, you must save your file in a macro-enabled format:

    1. Click File ➔ Save As (or press F12).
    2. In the Save as type drop-down menu, select Excel Macro-Enabled Workbook (*.xlsm).
    3. Click Save.

    ⚠️ Important Note: Standard .xlsx files cannot store VBA scripts. If you save as a normal .xlsx workbook, your macro code will be permanently deleted!