Why is delete blank rows vba not working?

topi1

Active Member
Joined
Aug 6, 2014
Messages
252
Office Version
  1. 2010
How come neither of the attached VBAs to delete empty rows in the attached sheet do not work? Thank you in advance for your help.

VBA Code:
Sub Delete_empty_rows()
Columns("C:C").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub

Rich (BB code):
Sub DeleteEmptyCellsInColumnC()
    Dim rng As Range
    Dim cell As Range

    Set rng = Range("C1:C" & Cells(Rows.Count, "C").End(xlUp).Row)

    For Each cell In rng
        If cell.Value = "" Or cell.Value = " " Then
            cell.Delete Shift:=xlUp
        End If
    Next cell
End Sub

RapidScribe.xlsm
C
1ABC
2
3LMN
4
5XYZ
6
7123
8QWE
9
10RTY
11456
12UIO
13
14
15ASD
16FGH
Sheet7
 
Column C has been populated by pasting values from another column which may have a formula. So some of the cells look blank but on LEN formula show 1.
What is the exact formula in the other column?
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try using the code below before the delete as long as you do need no formulas in the column

VBA Code:
Range("C:C").Value = Range("C:C").Value
 
Upvote 0
What is the exact formula in the other column?
@Peter_SSs the formula are not always same. Could be direct reference to a same row cell from another column, vlookup etc.

Since it says Len(c)=1, and not expecting one character in the C and keeping B empty, I came up with the following code. It works. A combination as you can tell. Refined but it seems to work. if all fails, I could it. Thank you.


VBA Code:
Sub DELETEALLBLANKROWS_()
  On Error Resume Next
  Columns("C").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

   Dim rng As Range
    Dim cell As Range

    ' Define the range for cells B1 to B250
    Set rng = Range("B1")

    ' Iterate through each cell in the defined range
    For Each cell In rng
        ' Add the desired formula to each cell
        cell.Formula = "=len(c1)"
    Next cell

Range("B1").Select
    Selection.Copy
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.AutoFill Destination:=Range("B1:B250"), Type:=xlFillDefault
    Range("B1:B250").Select


    Dim i As Long
    ' Start from the last cell and move upwards to prevent skipping cells after deletion
    Set rng = Range("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row)
    
    For i = rng.Cells(rng.Cells.Count).Row To 1 Step -1
        Set cell = rng.Worksheet.Cells(i, "B")
        If cell.Value = 1 Then
            cell.EntireRow.Delete
        End If
    Next i
    
    Columns("B:B").Select
    Selection.ClearContents
    Range("B1").Select

End Sub
 
Upvote 0
Try using the code below before the delete as long as you do need no formulas in the column

VBA Code:
Range("C:C").Value = Range("C:C").Value
@MARK858 you had educated me about this in the prior posting and I had tried it. Tried again. It did not work.
 
Upvote 0
@Peter_SSs the formula are not always same.
Well it sounds like the original formulas could be returning " " rather than "" otherwise the LEN would not be 1. First thing I would do is check that and change the formulas so they are retuning a zero length string rather than a space character. Then include the extra line Mark has suggested.
 
Upvote 0
That won't help if this is actually true ..
It is derived from a shared file. Perhaps, I can use a substitute formula to achieve what you have suggested. Will try. For now, got to run. I really appreciate the help!
 
Upvote 0
still trying to figure out what is going on. Meanwhile comforted by the fact that my convoluted vba works. Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,125
Members
453,021
Latest member
Justyna P

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