A better way to handle blanks

tourless

Board Regular
Joined
Feb 8, 2007
Messages
144
Office Version
  1. 365
Platform
  1. Windows
Hi Folks.

I have a routine that is supposed to look for blank cells (after there is a subtotal placed on the data) and fill them with data. My problem is sometimes there is only one subtotal so that leaves the 'filling in the blanks' problem mute. My routine doesn't know how to handle that and ends up failing. Please be gentle with this code, it's one of my earliest routines and looking back I'm cringing at the selects and lack of loops and such... completely inefficient. I am going through the whole 1,582 lines and updating but feel free to hack away at this routine. -Thanks.
VBA Code:
Sub FillBlanksA()

    Dim rRange1 As Range, rRange2 As Range
    Dim iReply As Integer

    'I've selected my worksheet earlier in the macro so I do know where I am
    'Check For Data
    Range("A:A").Select
    If Selection.Cells.Count = 1 Then
            Exit Sub
    ElseIf Selection.Columns.Count > 1 Then
            Exit Sub
    End If

    'Set Error Handling for Blank Cells
    Set rRange1 = Range(Selection.Cells(1, 1), Cells(65536, Selection.Column).End(xlUp))
    On Error Resume Next
    Set rRange2 = rRange1.SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0
    If rRange2 Is Nothing Then
        Exit Sub
    End If
   
    'Fill Blank Cells Column A
    rRange2.FormulaR1C1 = "=R[-1]C"
    rRange1 = rRange1.Value
    Cells(Rows.Count, "A").End(xlUp).Copy Cells(Rows.Count, "A").End(xlUp).Offset(1)
        
    Call FillBlanksC

End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi tourless,

what about

VBA Code:
Public Sub MrE_1222489_1614118()
' https://www.mrexcel.com/board/threads/a-better-way-to-handle-blanks.1222489/
' Created: 20221117
' By:      HaHoBe
' Version: 1
' Updated: no
' Reason:  none
  Dim rRange1             As Range
  Dim wsWork              As Worksheet
 
  Const cstrColLetter     As String = "A"
  
  On Error GoTo err_1222489
  
  Set wsWork = ActiveSheet
  With wsWork
    With .Range(.Cells(1, cstrColLetter), .Cells(.Rows.Count, cstrColLetter).End(xlUp))
      If WorksheetFunction.CountBlank(.Cells) > 0 Then
        Set rRange1 = .SpecialCells(xlCellTypeBlanks)
        rRange1.FormulaR1C1 = "=R[-1]C"
        rRange1.Value = rRange1.Value
        wsWork.Cells(wsWork.Rows.Count, cstrColLetter).End(xlUp).Offset(1).Value = _
            wsWork.Cells(wsWork.Rows.Count, cstrColLetter).End(xlUp).Value
        Set rRange1 = Nothing
      Else
        MsgBox "no blanks found on Sheet '" & wsWork.Name & " in Column '" & cstrColLetter & "', please check data", _
        vbInformation, "nothing to do here"
      End If
    End With
  End With

end_1222489:
  Set wsWork = Nothing
  Exit Sub

err_1222489:
  MsgBox "An error occurred." & vbCrLf & _
    "More information in the Immediate Window.", vbInformation, _
    "Error for MrE_1222489"

  Debug.Print "Error Procedure: MrE_1222489_1614118"
  Debug.Print "Error Time: " & Now()
  Debug.Print "Error Number: " & Err.Number
  Debug.Print "Error Description: " & Err.Description
  Err.Clear
  On Error GoTo 0
  Resume end_1222489:
End Sub

VBA Code:
Public Sub MrE_1222489_1614118_Vers2()
' https://www.mrexcel.com/board/threads/a-better-way-to-handle-blanks.1222489/
' Created: 20221117
' By:      HaHoBe
' Version: 1
' Updated: no
' Reason:  none
  Dim lngArr              As Long
  Dim rRange1             As Range
  Dim varCols             As Variant
  Dim wsWork              As Worksheet
  
  On Error GoTo err_1222489
  
  varCols = Array("A", "C")
  
  Set wsWork = ActiveSheet
  With wsWork
    For lngArr = LBound(varCols) To UBound(varCols)
      With .Range(.Cells(1, varCols(lngArr)), .Cells(.Rows.Count, varCols(lngArr)).End(xlUp))
        If WorksheetFunction.CountBlank(.Cells) > 0 Then
          Set rRange1 = .SpecialCells(xlCellTypeBlanks)
          rRange1.FormulaR1C1 = "=R[-1]C"
          rRange1.Value = rRange1.Value
          wsWork.Cells(wsWork.Rows.Count, varCols(lngArr)).End(xlUp).Offset(1).Value = _
              wsWork.Cells(wsWork.Rows.Count, varCols(lngArr)).End(xlUp).Value
          Set rRange1 = Nothing
        Else
          MsgBox "no blanks found on Sheet '" & wsWork.Name & " in Column '" & varCols(lngArr) & "', please check data", _
          vbInformation, "nothing to do here"
        End If
      End With
    Next lngArr
  End With

end_1222489:
  Set wsWork = Nothing
  Exit Sub

err_1222489:
  MsgBox "An error occurred." & vbCrLf & _
    "More information in the Immediate Window.", vbInformation, _
    "Error for MrE_1222489"

  Debug.Print "Error Procedure: MrE_1222489_1614118"
  Debug.Print "Error Time: " & Now()
  Debug.Print "Error Number: " & Err.Number
  Debug.Print "Error Description: " & Err.Description
  Err.Clear
  On Error GoTo 0
  Resume end_1222489:
End Sub

Ciao,
Holger
 
Upvote 0
Solution
Hi tourless,

what about

VBA Code:
Public Sub MrE_1222489_1614118()
[QUOTE="HaHoBe, post: 5978406, member: 14849"]

[/QUOTE]
[/QUOTE]
Looks like a winner. I'm going to keep testing by it looks very promising, thank you very much!
 
Upvote 0
Hi tourless,

thanks for the feedback, feel free to post back if anything should be altered.

Holger
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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