Fill Cells Between the First and Last 1 in Each Excel Row
Question details
Fill every cell between the first and last numeric 1 in each row using VBA or a dynamic-array formula.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A worksheet contains rows with one or more cells equal to 1, and the user wants the span between the outermost 1s filled.
- Observed behavior
- Each row should contain 1s from its first through its last 1, while cells outside that span remain unchanged.
Identify the source range and check whether cells between the 1s contain data or formulas you need to keep. The examples use A2:G11; adjust the ranges if your data is elsewhere.
Fill the Original Excel Cells With a VBA Macro
Use this macro to write 1 directly into the cells between the first and last numeric 1 in each row.
In desktop Excel, open Developer > Visual Basic, then select Insert > Module.
Paste this code into the module: Sub FillBetweenOnes() Dim ws As Worksheet Dim r As Long, c As Long Dim firstCol As Long, lastCol As Long Dim v As Variant Set ws = ActiveSheet For r = 2 To 11 firstCol = 0 lastCol = 0 For c = 1 To 7 v = ws.Cells(r, c).Value2 If Not IsError(v) Then If IsNumeric(v) And VarType(v) <> vbString Then If v = 1 Then If firstCol = 0 Then firstCol = c lastCol = c End If End If End If Next c If firstCol > 0 And lastCol > firstCol Then ws.Range(ws.Cells(r, firstCol), ws.Cells(r, lastCol)).Value = 1 End If Next r End Sub
Return to the worksheet, select Developer > Macros, choose FillBetweenOnes, and click Run. The macro processes A2:G11 on the active sheet.
To keep the macro in the workbook, use File > Save As and select Excel Macro-Enabled Workbook (.xlsm).

Create Filled Rows With a Microsoft 365 Dynamic-Array Formula
Use a formula when you want the filled result in a separate range without changing the source cells.
Fill Between 1s With a WPS Spreadsheets Formula
Use a formula in a separate output range to mark every cell from the first through the last 1 in each row, while leaving the source data unchanged.
- 1. Open the worksheet: Open the workbook in WPS Spreadsheets and identify the source range, such as A2:G11.
- 2. Enter the formula: In an empty cell such as J2, enter =IF(AND(COUNTIF($A2:A2,1)>0,COUNTIF(A2:$G2,1)>0),1,IF(A2="","",A2)).
- 3. Fill the output range: Drag J2's fill handle across to P2 and then down through P11. Each output cell checks for a 1 on both sides of its corresponding source cell.
- 4. Use the results: Review J2:P11. If you need fixed values in the original range, copy the results and use Paste Special > Values over A2:G11.

Frequently Asked Questions
What happens if a row has only one 1?
The VBA macro leaves that row unchanged. The formulas display the existing 1 and do not fill other cells in the row.
Will the VBA macro overwrite formulas between the 1s?
Yes. The macro writes 1 across the entire span, replacing any values or formulas there. Use the separate-output formula if you need to preserve the source cells.
Why does the Excel formula show a spill error?
Check that the output cells to the right of the formula are empty and do not overlap the source range. For the example in J2, clear J2:P2 before entering the formula.
How do I change the range processed by the macro?
In the VBA editor, change the row limits in For r = 2 To 11 and the column limits in For c = 1 To 7. Column 1 is A and column 7 is G.




