Hello there!
I am trying to write a macro that will remove cell rows that are empty after pressing a form control button. The cells that are 'empty' have an IF formula that returns blank "" if a certain criteria is not met, these are the cell rows I want to remove.
I've tried the following code but it deletes all cell rows, even when the cell returns an actual value.
I've also tried some along the lines of the following code, which doesn't delete the rows because they aren't technically blank, there is a formula in them.
I am trying to write a macro that will remove cell rows that are empty after pressing a form control button. The cells that are 'empty' have an IF formula that returns blank "" if a certain criteria is not met, these are the cell rows I want to remove.
I've tried the following code but it deletes all cell rows, even when the cell returns an actual value.
VBA Code:
Sub Button4_Click()
Dim sh As Worksheet, rngForm As Range, rngDel As Range, cF As Range
Set sh = ActiveSheet
On Error Resume Next
Set rngForm = sh.UsedRange.SpecialCells(xlCellTypeFormulas)
On Error GoTo 0
If rngForm Is Nothing Then Exit Sub
For Each cF In rngForm.Cells 'iterate between cells having formula
If cF.Value = "" Then 'find the ones returning the null strinig
If rngDel Is Nothing Then 'if rngDel not Set:
Set rngDel = cF 'Set it as first such a cell
Else
Set rngDel = Union(rngDel, cF) 'make a Union between the existing range and the cell returning a null string
End If
End If
Next
If Not rngDel Is Nothing Then rngDel.ClearContents 'it will clear the formula
End Sub
I've also tried some along the lines of the following code, which doesn't delete the rows because they aren't technically blank, there is a formula in them.
Last edited by a moderator: