VBA:删除范围内的空白单元格

问题描述 投票:0回答:4

我想删除范围内的空白单元格(E1:E130)。我尝试使用if cell value = ""然后可以在下面的代码中看到:

For Each cell In ranger
    If cell.Value = "" Then
        cell.Delete
    End If
next cell  

但是,此代码似乎跳过一些单元格而不删除所有想要的单元格。为了使我的目标更加明确:我目前有一个包含文本和空单元格的单元格列表,范围为E1:E130,我想在E1上创建一个没有任何空单元格的列表。例如,对字母表进行排序也是一个很好的解决方案,但这对我来说也不合适:S困惑

在此先感谢,布尔

vba excel-vba sorting cells excel
4个回答
3
投票

我会像以下一样

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

1
投票

您可以使用Range.SpecialCells Method一次删除特定范围内的所有空白单元格:

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

1
投票

您可以尝试使用此代码删除定义范围上的空白单元格:

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

0
投票

这应该工作:

Columns("E:E").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Delete Shift:=xlUp
© www.soinside.com 2019 - 2024. All rights reserved.