Macro to autofill intermittent blank cells to the right

alexcon

New Member
Joined
Dec 29, 2017
Messages
20
Greetings can anyone please assist with a button macro to autofill cells to the right as per the colored-in cells in screen clip below:


https://ibb.co/ew2Sbb


Whenever the value changes, it fills to the right according to that value, then when a new value is found, it fills to the right according to the new value and so on.
Would also like it cope when more columns and rows are added later.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
If you want fill to the last used column on the sheet :
Code:
Sub FillBlanks()
Dim lr%, lc%
lr = Cells(Rows.Count, "A").End(xlUp).Row
lc = Cells(1, Columns.Count).End(xlToLeft).Column
With Range([C2], Cells(lr, lc))
    On Error Resume Next
    .SpecialCells(xlCellTypeBlanks) _
        .FormulaR1C1 = "=IF(RC[-1]="""","""",RC[-1])"
    On Error GoTo 0
    .Value = .Value
End With
End Sub

If you want to fill each row to the last value in each row :
Code:
Sub FillBlanks2()
Dim lr%, r%, lc%
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To lr
    lc = Cells(r, Columns.Count).End(xlToLeft).Column
    On Error Resume Next
    Range(Cells(r, "C"), Cells(r, lc)).SpecialCells(xlCellTypeBlanks) _
        .FormulaR1C1 = "=IF(RC[-1]="""","""",RC[-1])"
    On Error GoTo 0
Next
With Range([B2], Cells(lr, ActiveSheet.UsedRange.Columns.Count))
    .Value = .Value
End With
End Sub
 
Upvote 0
If you want fill to the last used column on the sheet :
Code:
Sub FillBlanks()
Dim lr%, lc%
lr = Cells(Rows.Count, "A").End(xlUp).Row
lc = Cells(1, Columns.Count).End(xlToLeft).Column
With Range([C2], Cells(lr, lc))
    On Error Resume Next
    .SpecialCells(xlCellTypeBlanks) _
        .FormulaR1C1 = "=IF(RC[-1]="""","""",RC[-1])"
    On Error GoTo 0
    .Value = .Value
End With
End Sub

If you want to fill each row to the last value in each row :
Code:
Sub FillBlanks2()
Dim lr%, r%, lc%
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To lr
    lc = Cells(r, Columns.Count).End(xlToLeft).Column
    On Error Resume Next
    Range(Cells(r, "C"), Cells(r, lc)).SpecialCells(xlCellTypeBlanks) _
        .FormulaR1C1 = "=IF(RC[-1]="""","""",RC[-1])"
    On Error GoTo 0
Next
With Range([B2], Cells(lr, ActiveSheet.UsedRange.Columns.Count))
    .Value = .Value
End With
End Sub

Thank you very much for this!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top