【问题标题】:Using loop in VBA to delete a range and changing the range every iteration在 VBA 中使用循环删除范围并在每次迭代时更改范围
【发布时间】:2020-10-30 14:45:07
【问题描述】:

我想在 Excel (A50:B80) 中选择一个范围并删除其所有内容。我将保留两行,然后我希望下一个循环迭代从 (A52:B82) 开始,依此类推。这是我目前所拥有的:

Sub Macro2() 
 Dim i As Integer
 Dim num As Integer
 num = 50
    
 Dim num_2 As Integer
 num_2 = 80
    
 For i = 1 To 2:
     Set Range1 = Range("A" & num)
     Set Range2 = Range("B" & num_2)
     Range(Range1, Range2).Select
     Selection.delete Shift:=x1Up
     num = num + 2
     num_2 = num + 2
 Next i
End Sub

但我不断收到“运行时错误 '1004:Range 类的删除方法失败。

任何帮助将不胜感激!

【问题讨论】:

  • 不要那样做。使用Union 构造一个要删除的范围,然后一步删除.. 就像演示的here
  • 你必须将循环向后运行,或者将所有要删除的范围放在一个联合范围中,最后将其删除。这样,它删除了第一个范围,然后它失去了引用,因为之前的删除......
  • @FaneDuru 我明白你在暗示什么,但在我第一次删除初始范围后,将保留 2 行。因此,如果循环再次从第一个范围集开始,它将删除我想要保留的两行。或者我误解了你的意思

标签: excel vba loops range


【解决方案1】:

这写错了:

Selection.delete Shift:=x1Up

尝试将其替换为:

Selection.Delete Shift:=xlUp

【讨论】:

    【解决方案2】:

    删除一系列范围

    • 在不确定要删除的内容之前,您不想删除任何内容。因此,在测试代码时始终使用Select

    守则

    Option Explicit
    
    Sub QuickFix()
        
        Dim First As Long
        First = 50
        Dim Last As Long
        Last = 80
        Dim i As Long
        
        Dim Range1 As Range
        Dim Range2 As Range
        Dim rng As Range
        Dim dRng As Range
        
        For i = 1 To 2
            Set Range1 = Range("A" & First)
            Set Range2 = Range("B" & Last)
            Set rng = Range(Range1, Range2)
            If Not dRng Is Nothing Then
                Set dRng = Union(dRng, rng)
            Else
                Set dRng = rng
            End If
            First = First + rng.Rows.Count + 2
            Last = Last + rng.Rows.Count + 2
        Next i
        
        If Not dRng Is Nothing Then
            dRng.Select
            Debug.Print dRng.Address
        End If
    
    End Sub
    
    Sub deleteRangesWithOffset()
        
        ' Define constants. All these you can change and see the differences.
        Const FirstRow As Long = 50
        Const LastRow As Long = 80
        Const FirstCol As String = "A"
        Const LastCol As String = "B"
        Const NumOfEmptyRows As Long = 2
        Const NumOfRanges As Long = 2
           
        ' Define Initial Range ('rng').
        Dim rng As Range
        Set rng = Range(FirstCol & FirstRow & ":" & LastCol & LastRow)
        ' or e.g.
        'Set rng = Range(Cells(FirstRow, FirstCol), Cells(LastRow, LastCol))
        ' or just...
        'Set rng = Range("A50:B80")
        
        Dim cRng As Range ' Current Range
        Dim dRng As Range ' Delete Range
        Dim i As Long     ' Current Ranges Counter
        
        For i = 1 To NumOfRanges
            ' Define Current Range.
            Set cRng = rng.Offset((i - 1) * (rng.Rows.Count + NumOfEmptyRows))
            ' This is how you combine (collect) the ranges into 'dRng'.
            If Not dRng Is Nothing Then
                Set dRng = Union(dRng, cRng)
            Else
                Set dRng = cRng
            End If
        Next i
        
        ' Check if there was any range 'collected' (Here it is not necessary).
        If Not dRng Is Nothing Then
            ' Test with select to see what is happening.
            ' Now maybe increase NumOfRanges to 4 and/or NumOfRanges and see
            ' what is selected... etc.
            ' Only later use 'Delete'.
            dRng.Select
            Debug.Print rng.Address ' See the range address in the Immediate window.
        End If
        
    End Sub
    

    【讨论】:

    • 这个太棒了!谢谢!它正在选择我要删除的内容。我应该输入什么来删除选择的内容?当我把它放在 dRng.Select 之后时,Selection.Delete 不起作用
    • @McMckinson:用dRng.Delete 代替dRng.SelectSelect 只是一个预防措施,因为有时很难定义范围,所以你用Select 进行测试。否则Select 将被避免,因为您可以在本文开头的链接中找到。
    • 太棒了!我明白你现在的意思了。那么如果我删除它,单元格会自动向上移动吗?还是我必须添加命令?我很抱歉听起来有点无知哈哈,我学习 VBA 已经有一段时间了,现在正在实习,并且有一个项目可以帮助这种自动化。
    • @McMckinson:坦白说我不知道​​。但是你应该创建一个你的项目的副本,然后尝试不同的东西,比如Shift:=xlUp(记住它是xlUp中的字母'l')。我通常只删除您可以使用dRng.EntireRow.Delete 实现的完整行。 VBA 或 Excel 中有很多选项,您无法记住所有内容,因此您可以像以前一样在 Internet 上查找当前需要的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    相关资源
    最近更新 更多