VBA & Macro Problems

Fill Excel Column Values Until a Criterion Is Met with VBA

Camila MilosovichCamila Milosovich Dec 2, 2026 869 views

Question details

Fill column A with one value until a criterion appears in column B, then use a second value.

Fill Excel Column Values Until a Criterion Is Met with VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
Starting at row 6, use the value in F2 until column B contains Entity_ID, then use the value in F3.
Observed behavior
Column A should switch to the F3 value on the Entity_ID row and keep that value for subsequent rows.
Before you start

Enter the initial value in F2 and the replacement value in F3, and check that the data starts in row 6. The macro processes through the last nonempty cell in column B and overwrites existing values in column A.

Solution 1Recommended

Use a VBA loop to fill column A

Use this macro in desktop Excel to write values directly into column A.

The loop starts with F2 and switches to F3 as soon as it finds Entity_ID in column B. It keeps using F3 for the remaining rows.

1
Open the VBA editor

In desktop Excel, open the workbook, select the worksheet to fill, and choose Developer > Visual Basic.

2
Insert the macro

In the VBA editor, choose Insert > Module and paste: Sub FillA() Dim ws As Worksheet Dim r As Long, lastRow As Long Dim currentValue As Variant Set ws = ActiveSheet lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row currentValue = ws.Range("F2").Value For r = 6 To lastRow If ws.Cells(r, "B").Value = "Entity_ID" Then currentValue = ws.Range("F3").Value ws.Cells(r, "A").Value = currentValue Next r End Sub

3
Run the macro

Return to Excel, select the intended worksheet, then choose Developer > Macros, select FillA, and click Run.

4
Save the workbook

Choose File > Save As and select Excel Macro-Enabled Workbook (.xlsm) if you want to keep the macro.

Use a VBA loop to fill column A
Check the marker: The macro checks for Entity_ID as the cell value, not as part of a longer string. If the marker never appears, every processed row receives the F2 value.
WPS Spreadsheets

Fill values until a marker appears in WPS Spreadsheets

Use a worksheet formula in WPS Spreadsheets to switch column A from the F2 value to the F3 value when Entity_ID appears in column B, without running a macro.

  1. 1. Open the spreadsheet: Open your workbook in WPS Spreadsheets and select the worksheet containing your data.
  2. 2. Check the source cells: Enter the initial value in F2, the replacement value in F3, and the Entity_ID marker in the appropriate cell in column B.
  3. 3. Enter the formula: Select A6 and enter =IF(COUNTIF(B$6:B6,"Entity_ID"),$F$3,$F$2).
  4. 4. Fill down: Drag the fill handle from A6 down to the last row you want to populate, then save the workbook.
Fill a column based on the first Entity_ID markerUpdate results automatically when source values changeOpen and edit Microsoft Excel XLSX filesUse a free spreadsheet editor with familiar tools
microsoft office alternative - wps office

Frequently Asked Questions

Does the value change on the Entity_ID row or the next row?

Both the macro and the formula use the F3 value on the row where column B first equals Entity_ID.

What happens if Entity_ID never appears?

The macro fills the processed rows with F2. The formula also returns F2 in each row where it is filled.

How do I fill rows beyond the last nonempty cell in column B?

The macro stops at the last nonempty cell in column B. To include additional rows, change the lastRow assignment in the VBA editor to the desired row number, such as lastRow = 100.

Can I use the formula without enabling macros?

Yes. Enter =IF(COUNTIF(B$6:B6,"Entity_ID"),$F$3,$F$2) in A6 and fill it down; no VBA or macro settings are needed.