VBA FindAll方法在带delete的范围内

VBA FindAll方法在带delete的范围内,vba,excel,Vba,Excel,结果:查找并删除某个范围内的值,然后将单元格上移 sub(FindAllDelete) Dim EmptyCells As Range Dim LastCell As Range Dim FirstAddr As String With Worksheets("Sheet1").Range("F30:W1000") Set LastCell = Range("W1000") End With Set EmptyCells = Worksheets("Sheet1").Range("F30:W

结果:查找并删除某个范围内的值,然后将单元格上移

sub(FindAllDelete)

Dim EmptyCells As Range
Dim LastCell As Range
Dim FirstAddr As String

With Worksheets("Sheet1").Range("F30:W1000")
Set LastCell = Range("W1000")
End With
Set EmptyCells = Worksheets("Sheet1").Range("F30:W1000").Find(What:="0", After:=LastCell)

If Not EmptyCells Is Nothing Then
    FirstAddr = LastCell.Address
End If

  Do Until EmptyCells Is Nothing
     Debug.Print EmptyCells.Address
     Set EmptyCells = Worksheets("Sheet1").Range("F30:W1000").FindNext(after=EmptyCells)
    If EmptyCells.Address = FirstAddr Then
  Exit Do
    End If
    Loop
    EmptyCells.Delete Shift:=xlShiftUp
我的代码中有一些关于无限循环的问题。我相信问题可能出在最后一个电池上

我需要定位范围内的所有0。选择、删除并上移单元格

sub(FindAllDelete)

Dim EmptyCells As Range
Dim LastCell As Range
Dim FirstAddr As String

With Worksheets("Sheet1").Range("F30:W1000")
Set LastCell = Range("W1000")
End With
Set EmptyCells = Worksheets("Sheet1").Range("F30:W1000").Find(What:="0", After:=LastCell)

If Not EmptyCells Is Nothing Then
    FirstAddr = LastCell.Address
End If

  Do Until EmptyCells Is Nothing
     Debug.Print EmptyCells.Address
     Set EmptyCells = Worksheets("Sheet1").Range("F30:W1000").FindNext(after=EmptyCells)
    If EmptyCells.Address = FirstAddr Then
  Exit Do
    End If
    Loop
    EmptyCells.Delete Shift:=xlShiftUp

您的
EmptyCells.Delete Shift:=xlShiftUp
位于
Do
循环之外,因此在单元上循环期间不会执行它。如果您一致地缩进代码,则更容易发现此类错误,例如:

Do Until EmptyCells Is Nothing
    Debug.Print EmptyCells.Address
    Set EmptyCells = Worksheets("Sheet1").Range("F30:W1000").FindNext(after=EmptyCells)
    If EmptyCells.Address = FirstAddr Then
        Exit Do
    End If
Loop
EmptyCells.Delete Shift:=xlShiftUp

将其格式化为这样,一眼就可以看出.Delete并不是发生在所有单元格上,而是在最后只发生一次。

您的
清空单元格.Delete Shift:=xlShiftUp
位于
Do
循环之外,因此在单元格上循环时不会执行它。如果您一致地缩进代码,则更容易发现此类错误,例如:

Do Until EmptyCells Is Nothing
    Debug.Print EmptyCells.Address
    Set EmptyCells = Worksheets("Sheet1").Range("F30:W1000").FindNext(after=EmptyCells)
    If EmptyCells.Address = FirstAddr Then
        Exit Do
    End If
Loop
EmptyCells.Delete Shift:=xlShiftUp

将其格式化为这样可以一眼看出,并非所有单元格都会发生.Delete,但最后只会发生一次。

如果范围(“F30:W1000”)中的所有单元格都有值(开头没有空白值),则以下代码将正常工作

首先,将范围内所有包含“0”的单元格替换为“”,因此现在它们实际上为空

现在,使用
SpecialCells(xlCellTypeBlanks)
我们可以设置另一个范围,并一次删除所有范围

注意:就像我在开头写的那样,只有当范围内的所有单元格都有值时,这才有效,如果有空单元格(PO说没有),它们也将被删除

代码

Sub FindAllDelete()

Dim Rng As Range
Dim EmptyCells As Range

With Worksheets("Sheet1") '<-- modify "Sheet1" to your sheet's name
    Set Rng = .Range("F30:W1000")

    With Rng
        .Replace What:="0", Replacement:="", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False
    End With

    Set EmptyCells = Rng.SpecialCells(xlCellTypeBlanks)
    EmptyCells.Delete xlShiftUp
End With

End Sub
子FindAllDelete()
变暗Rng As范围
暗淡的空细胞作为射程

对于工作表(“Sheet1”)如果范围(“F30:W1000”)中的所有单元格都有值(开头没有空白值),则以下代码将正常工作

首先,将范围内所有包含“0”的单元格替换为“”,因此现在它们实际上为空

现在,使用
SpecialCells(xlCellTypeBlanks)
我们可以设置另一个范围,并一次删除所有范围

注意:就像我在开头写的那样,只有当范围内的所有单元格都有值时,这才有效,如果有空单元格(PO说没有),它们也将被删除

代码

Sub FindAllDelete()

Dim Rng As Range
Dim EmptyCells As Range

With Worksheets("Sheet1") '<-- modify "Sheet1" to your sheet's name
    Set Rng = .Range("F30:W1000")

    With Rng
        .Replace What:="0", Replacement:="", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False
    End With

    Set EmptyCells = Rng.SpecialCells(xlCellTypeBlanks)
    EmptyCells.Delete xlShiftUp
End With

End Sub
子FindAllDelete()
变暗Rng As范围
暗淡的空细胞作为射程

对于工作表(“Sheet1”)”我更喜欢@Shai Rado给出的答案,因为它不涉及任何循环,但我说我写了代码,所以我要发布它

这样可以合并所有0范围,或者您可以在找到它们时删除它们,然后继续循环,直到
EmptyCells为Nothing

Public Sub FindAllDelete()

    Dim EmptyCells As Range
    Dim AllCells As Range
    Dim FirstAddr As String

    With Worksheets("Sheet1").Range("F30:W1000")
        Set EmptyCells = .Find(0)

        If Not EmptyCells Is Nothing Then
            FirstAddr = EmptyCells.Address
            Do
                If EmptyCells.Address = FirstAddr Then
                    Set AllCells = EmptyCells
                Else
                    Set AllCells = Union(AllCells, EmptyCells)
                End If

                Set EmptyCells = .FindNext(EmptyCells)

            Loop While EmptyCells.Address <> FirstAddr
        End If

    End With

    AllCells.Delete Shift:=xlShiftUp

End Sub
公共子FindAllDelete()
暗淡的空细胞作为射程
将所有单元格设置为范围
Dim FirstAddr作为字符串
带有工作表(“表1”)。范围(“F30:W1000”)
设置EmptyCells=.Find(0)
如果不是空的话,那就什么都不是了
FirstAddr=EmptyCells.Address
做
如果EmptyCells.Address=FirstAddr,则
设置所有单元格=空单元格
其他的
设置所有单元格=并集(所有单元格,空单元格)
如果结束
设置EmptyCells=.FindNext(EmptyCells)
清空时循环。地址FirstAddr
如果结束
以
AllCells.Delete Shift:=xlShiftUp
端接头

我更喜欢@Shai Rado给出的答案,因为它不涉及任何循环,但我说我写了代码,所以我要发布它

这样可以合并所有0范围,或者您可以在找到它们时删除它们,然后继续循环,直到
EmptyCells为Nothing

Public Sub FindAllDelete()

    Dim EmptyCells As Range
    Dim AllCells As Range
    Dim FirstAddr As String

    With Worksheets("Sheet1").Range("F30:W1000")
        Set EmptyCells = .Find(0)

        If Not EmptyCells Is Nothing Then
            FirstAddr = EmptyCells.Address
            Do
                If EmptyCells.Address = FirstAddr Then
                    Set AllCells = EmptyCells
                Else
                    Set AllCells = Union(AllCells, EmptyCells)
                End If

                Set EmptyCells = .FindNext(EmptyCells)

            Loop While EmptyCells.Address <> FirstAddr
        End If

    End With

    AllCells.Delete Shift:=xlShiftUp

End Sub
公共子FindAllDelete()
暗淡的空细胞作为射程
将所有单元格设置为范围
Dim FirstAddr作为字符串
带有工作表(“表1”)。范围(“F30:W1000”)
设置EmptyCells=.Find(0)
如果不是空的话,那就什么都不是了
FirstAddr=EmptyCells.Address
做
如果EmptyCells.Address=FirstAddr,则
设置所有单元格=空单元格
其他的
设置所有单元格=并集(所有单元格,空单元格)
如果结束
设置EmptyCells=.FindNext(EmptyCells)
清空时循环。地址FirstAddr
如果结束
以
AllCells.Delete Shift:=xlShiftUp
端接头

这可以在没有VBA的情况下完成,您可以使用CTRL+H将0替换为空白,然后使用CTRL+G删除空白选择空白并删除

这可以在没有VBA的情况下完成,您可以使用CTRL+H将0替换为空白,然后使用CTRL+G删除空白选择空白并删除

您需要定义哪个工作表,如
工作表(“Sheet1”)
。但是你在这里倾诉了很多事情,你到底想要实现什么?从
范围(“F30:W1000”)中删除所有“0”
?嗨,我确实忘记在帖子中添加工作表了,但它在我的代码中。是,我想查找所有0,然后删除单元格并将其上移。该范围内的所有其他单元格是否都有值?或者你有一些空单元格?其他单元格不是空的。它们有值。您需要定义哪个工作表,如
工作表(“Sheet1”)
。但是你在这里倾诉了很多事情,你到底想要实现什么?从
范围(“F30:W1000”)中删除所有“0”
?嗨,我确实忘记在帖子中添加工作表了,但它在我的代码中。是,我想查找所有0,然后删除单元格并将其上移。该范围内的所有其他单元格是否都有值?或者你有一些空单元格?其他单元格不是空的。它们有值。(注释仅适用于第1个版本)注意,这也会删除在此之前为空的所有单元格;e、 g.它删除所有“0”单元格和所有空单元格。请参阅我上面的注释^(com