VBA: Delete blank cells in range

喜你入骨 提交于 2019-12-01 12:18:58

问题


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


回答1:


I'd go like follows

With Range("E1:E130")
    If WorksheetFunction.CountA(.Cells) > 0 Then .SpecialCells(xlCellTypeBlanks).Delete Shift:=xlShiftUp
End With



回答2:


You could use the Range.SpecialCells Method to delete all blank cells in a specific range at once:

Range("E1:E130").SpecialCells(xlCellTypeBlanks).Delete



回答3:


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



回答4:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!