add additional columns to this macro

steve400243

Active Member
Joined
Sep 15, 2016
Messages
429
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello, I use this VBA to fill blanks in column D. I need Column's B, C, and G to also have the blanks fill

VBA Code:
Sub Fill_Blanks()
  With Range("D3:D" & ActiveSheet.UsedRange.Rows.Count)
    .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
    .Value = .Value
  End With
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try this:
VBA Code:
Sub Fill_Blanks()

    Dim cols()
    Dim i As Long
    Dim lr As Long
    Dim rng As Range
   
'   Set columns to update
    cols = Array("B", "C", "D", "G")
   
    Application.ScreenUpdating = False
   
'   Find last row on sheet with data
    lr = ActiveSheet.UsedRange.Rows.Count
   
'   Loop through columns
    For i = LBound(cols) To UBound(cols)
'       Build range
        Set rng = Range(Cells(3, cols(i)), Cells(lr, cols(i)))
'       Fill blanks
        On Error Resume Next
        With rng
            .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
            .Value = .Value
        End With
        On Error GoTo 0
    Next i
   
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 0
Solution
Try this:
VBA Code:
Sub Fill_Blanks()

    Dim cols()
    Dim i As Long
    Dim lr As Long
    Dim rng As Range
  
'   Set columns to update
    cols = Array("B", "C", "D", "G")
  
    Application.ScreenUpdating = False
  
'   Find last row on sheet with data
    lr = ActiveSheet.UsedRange.Rows.Count
  
'   Loop through columns
    For i = LBound(cols) To UBound(cols)
'       Build range
        Set rng = Range(Cells(3, cols(i)), Cells(lr, cols(i)))
'       Fill blanks
        On Error Resume Next
        With rng
            .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
            .Value = .Value
        End With
        On Error GoTo 0
    Next i
  
    Application.ScreenUpdating = True
  
End Sub
Excellent, Thank you for providing this. I appreciate your time.
 
Upvote 0
You are welcome.
Glad I was able to help.

And if you ever want to add or remove columns, all you have to do is edit the values in the array (don't need to add any lines of code, just edit that one that lists all the columns to include).
 
Upvote 0
You are welcome.
Glad I was able to help.

And if you ever want to add or remove columns, all you have to do is edit the values in the array (don't need to add any lines of code, just edit that one that lists all the columns to include).
Thank you, was just thinking about that.
 
Upvote 0

Forum statistics

Threads
1,215,426
Messages
6,124,829
Members
449,190
Latest member
rscraig11

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