Home » excel » excel – Fill down blank cells in multiple columns in specific sheets

excel – Fill down blank cells in multiple columns in specific sheets

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have the following columns
enter image description here

Score   Name    Last Name
5460    Name1   Lastname1
2620        
340     
470     
3445    Name2   Lastname2
1290        
1170        
7460    Name3   Lastname3
3520        
360     
1500        
5048    Name4   Lastname4
830     
490     
1883        
620     
1208        
185     
426     
653     

I want to fill down the empty cells in columns B & C until the last row with in Column A (which is row 21 in this example).

Result

enter image description here

Score   Name    Last Name
5460    Name1   Lastname1
2620    Name1   Lastname1
340     Name1   Lastname1
470     Name1   Lastname1
3445    Name2   Lastname2
1290    Name2   Lastname2
1170    Name2   Lastname2
7460    Name3   Lastname3
3520    Name3   Lastname3
360     Name3   Lastname3
1500    Name3   Lastname3
5048    Name4   Lastname4
830     Name4   Lastname4
490     Name4   Lastname4
1883    Name4   Lastname4
620     Name4   Lastname4
1208    Name4   Lastname4
185     Name4   Lastname4
426     Name4   Lastname4
653     Name4   Lastname4

Currently I am trying to adapt this vba code:

Sub FillColBlanksSpecial()

Dim wks As Worksheet
Dim rng As Range
Dim rng2 As Range
Dim LastRow As Long
Dim col As Long
Dim lRows As Long
Dim lLimit As Long

Dim lCount As Long
On Error Resume Next

lRows = 2
lLimit = 1000

Set wks = ActiveSheet
With wks

   col = .Range("b1,c1").Column

   Set rng = .UsedRange  'try to reset the lastcell
   LastRow = .Cells.SpecialCells(xlCellTypeLastCell).Row
   Set rng = Nothing

    lCount = .Columns(col).SpecialCells(xlCellTypeBlanks).Areas(1).Cells.Count

    If lCount = 0 Then
        MsgBox "No blanks found in selected column"
        Exit Sub
    ElseIf lCount = .Columns(col).Cells.Count Then
        Do While lRows < LastRow
            Set rng = .Range(.Cells(lRows, col), .Cells(lRows + lLimit, col)) _
                           .Cells.SpecialCells(xlCellTypeBlanks)
            rng.FormulaR1C1 = "=R[-1]C"
            lRows = lRows + lLimit
        Loop
    Else
        Set rng = .Range(.Cells(2, col), .Cells(LastRow, col)) _
                       .Cells.SpecialCells(xlCellTypeBlanks)
        rng.FormulaR1C1 = "=R[-1]C"
    End If
End With

End Sub

Question1: this code only runs on Column B. How to run in in Column B & C
As you see in the code, I tried col = .Range("b1,c1").Column, but to no avail.

Question2: how to run this in sheets whose name end in -A and -B only in the same workbook?

In the same workbook, I have hundrends of sheets, but I want to run this only in those ending in the above suffixes.
Thanks.

How to&Answers:

You can use the same variable that you were using for the active sheet in a loop to cycle through the worksheet collection, checking the worksheet name to see if it requires processing.

Sub FillColBlanksSpecial()
    Dim wks As Worksheet

    For Each wks In Worksheets
        If Right(wks.Name, 2) = "-A" Or Right(wks.Name, 2) = "-B" Then
            With wks
                With .Cells(1, 1).CurrentRegion
                    With .Columns("B:C")
                        If CBool(Application.CountBlank(.Cells)) Then
                            .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=r[-1]c"
                        End If
                    End With
                    'un comment the next line if you want the formulas to revert to values only
                    '.Cells = .Cells.Value
                End With
            End With
        End If
    Next wks

End Sub

I’ve added a line to revert the duplicating formulas to their underlying values but left it commented. Uncomment the line if you prefer values only.