Use Excel VBA to Fill Blank Cells with the Previous Value
Question details
Fill blank cells in a column with the preceding value and convert the results to static values.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A column contains repeated labels or grouping data with blank cells between values.
- Observed behavior
- Each blank cell should inherit the value above it, with the processed column stored as values rather than formulas.
Identify the column to fill and make sure the cell immediately above the first row to process contains the starting value. The example uses column X, starts at row 2, and stops at the last nonempty cell in that column.
Run a VBA macro to fill blanks and replace formulas with values
Use this macro in desktop Excel to fill true blank cells in column X and convert the processed range to static values.
The macro works on the active worksheet. Change every column reference from X to your target column, and adjust row 2 if your data starts elsewhere.
In desktop Excel, select the worksheet to process, then open Developer > Visual Basic. If Developer is hidden, enable it under File > Options > Customize Ribbon.
In the VBA editor, choose Insert > Module and paste: Sub AutoFillBlanks() Dim ws As Worksheet Dim blankCells As Range Dim lastRow As Long Dim wasScreenUpdating As Boolean Set ws = ActiveSheet wasScreenUpdating = Application.ScreenUpdating On Error GoTo CleanUp Application.ScreenUpdating = False lastRow = ws.Cells(ws.Rows.Count, "X").End(xlUp).Row If lastRow < 2 Then GoTo CleanUp On Error Resume Next Set blankCells = ws.Range("X2:X" & lastRow).SpecialCells(xlCellTypeBlanks) On Error GoTo CleanUp If Not blankCells Is Nothing Then blankCells.FormulaR1C1 = "=R[-1]C" Application.Calculate ws.Range("X2:X" & lastRow).Value = ws.Range("X2:X" & lastRow).Value End If CleanUp: Application.ScreenUpdating = wasScreenUpdating End Sub
Return to the target worksheet, open Developer > Macros, select AutoFillBlanks, and click Run. Check that the blank cells contain the expected labels.
Choose File > Save As and select Excel Macro-Enabled Workbook (.xlsm) if you want to keep the macro.

Fill repeated column values in WPS Spreadsheets
Use a helper column to carry each preceding value down, then paste the results back as static values without running VBA.
- 1. Open the workbook: Open the workbook in WPS Spreadsheets. For this example, confirm that column X contains the data, X1 contains the starting value, and column Y is unused.
- 2. Create the helper formulas: Enter =X1 in Y1. In Y2, enter =IF(ISBLANK(X2),Y1,X2).
- 3. Fill the helper column: Select Y2 and drag its fill handle down to the last row you want to process. Each true blank in column X will use the previous helper value.
- 4. Paste static values: Copy the filled cells in column Y starting at Y2. Select X2, then use Paste Special > Values to replace the corresponding cells in column X with static results.
- 5. Check and clean up: Check the values in column X, then clear the helper cells in column Y if they are no longer needed. Use File > Save As to save the workbook.

Frequently Asked Questions
How do I use the macro on a different column?
In the VBA editor, replace each X column reference in the macro with your target column letter. Update the starting row in both range references if needed.
Why didn't the macro fill cells that look blank?
SpecialCells(xlCellTypeBlanks) finds truly empty cells, not formulas that return an empty string or cells containing spaces. Inspect those cells in the formula bar before running the macro.
Will the macro fill blank rows after the last label?
No. It determines the last row from the last nonempty cell in the target column. To include trailing blank rows, specify a different end row or determine it from another populated column.
What happens to existing formulas in the target range?
If the macro finds blanks, it replaces the entire processed range with values, including any existing formulas. The WPS helper-column method also replaces the pasted target range with values.
Why can't I keep the macro in an XLSX file?
XLSX does not store VBA macros. In Excel, use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm) to retain the macro.




