I want to delete blank cells in a range (E1:E130).
I tried to use if cell value = ""
then as can be seen in the code below:
For Each cell In ranger
If cell.Value = "" Then
cell.Delete
End If
next cell
However, this code seems to skip some cells and not delete all the wanted cells. To make my objective more clear: I currently have a list of cells with text and empty cells in the range E1:E130 and I want to make a list starting on E1 without any empty cells. Sorting on alphabet for instance would also be a good solution, however that didn't work out for me either :S confused
Thanks in advance, Boolean
I'd go like follows
With Range("E1:E130")
If WorksheetFunction.CountA(.Cells) > 0 Then .SpecialCells(xlCellTypeBlanks).Delete Shift:=xlShiftUp
End With
You could use the Range.SpecialCells Method to delete all blank cells in a specific range at once:
Range("E1:E130").SpecialCells(xlCellTypeBlanks).Delete
You can try with this code to remove blank cell on define range :
Sub RemoveBlankCells()
Dim rng As Range
'Store blank cells inside a variable
On Error GoTo NoBlanksFound
Set rng = Range("E1:E130").SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
'Delete blank cells and shift upward
rng.Rows.Delete Shift:=xlShiftUp
Exit Sub
'ERROR HANLDER
NoBlanksFound:
MsgBox "No Blank cells were found"
End Sub
This should work:
Columns("E:E").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Delete Shift:=xlUp
来源:https://stackoverflow.com/questions/49045885/vba-delete-blank-cells-in-range