Files
zmVault/excel-macros.md
T

1.8 KiB

id, tags, title, up
id tags title up
excel-macros
topic/software
type/guide
status/complete
Excel Macros excel

Excel Macros

Excel macros are a poor solution for most problems, however some simple formatting motions are a good fit.

This document is a step-by-step process for implementing such a macro, assuming a fresh Excel install.

1. Enable Developer Tools

Add the hidden-by-default "Developer Tools" menu to the ribbon.

File > Options > Customize Ribbon

On the right side, under Main Tabs, check Developer.

From the new menu you can access macros and VB projects.

2. Create PERSONAL.XLSB

Create an empty macro in your "Personal Macro Workbook".

Developer > Code > Record Macro

Set Store macro in: to Personal Macro Workbook and click OK, then Stop Recording.

This will create your PERSONAL.XLSB workbook, a special workbook whose macros and other VB code can be accessed from any other workbook.

3. Create a New Macro

Developer > Visual Basic

In the pop-up window, navigate to and double-click VBAProject (PERSONAL.XLSB)/Modules/Module1

Erase the empty macro created in step 2 and add any custom macros here. My favorite is provided below.

' Use on a column of empty and non-empty cells.
' Replace empty cells with the last non-empty cell's values
Sub FillDownEmptyCells()
    Dim rng As Range
    Dim cell As Range

    ' Check if a range is selected
    If Selection Is Nothing Then
        Exit Sub
    End If

    ' Set the selected range
    Set rng = Selection

    For Each cell In rng
        If IsEmpty(cell.Value) Then
            ' Copy cell above to empty cell
            cell.Value = cell.Offset(-1, 0).Value
        End If
    Next cell

End Sub

Use Ctrl+S or the save icon to save the edits then close the Visual Basic window.