繁体   English   中英

VBA代码运行不一致

[英]VBA Code runs inconsistently

嗨,我有这段代码,允许删除一个工作表中的单元格,此后,删除将在另一工作表中进行更新。 这段代码一直运行良好,直到今天我再次尝试时,它始终在Set delRange = Union(delRange, .Cells(j, i))处给我错误。 错误消息是object_'Global'failed的“ Method'Union”失败,我在其他工作簿上尝试了它,起初它起作用了,随后又再次给出了相同的错误。 我可以知道为什么会收到此错误,并且有什么解决方案可以调试吗?谢谢

Sub Database()

Dim rng As Range, rngError As Range, delRange As Range
Dim i As Long, j As Long, k As Long
Dim wks As Worksheet

On Error Resume Next

Set rng = Application.InputBox("Select cells To be deleted", Type:=8)

On Error GoTo 0

If rng Is Nothing Then Exit Sub Else rng.Delete

For k = 1 To ThisWorkbook.Worksheets.Count 'runs through all worksheets

  Set wks = ThisWorkbook.Worksheets(k)

  With wks

    For i = 1 To 7 '<~~ Loop trough columns A to G

        '~~> Check if that column has any errors
        On Error Resume Next

        Set rngError = .Columns(i).SpecialCells(xlCellTypeFormulas, xlErrors)

        On Error GoTo 0

        If Not rngError Is Nothing Then
            For j = 1 To 100 '<~~ Loop Through rows 1 to 100
                If .Cells(j, i).Text = "#REF!" Then
                    '~~> Store The range to be deleted

             If delRange Is Nothing Then
              Set delRange = .Cells(j, i)

               Else
            Set delRange = Union(delRange, .Cells(j, i))


                    End If
                End If
             Next j
         End If

     Next i

  End With

Next k

'~~> Delete the range in one go
If Not delRange Is Nothing Then delRange.Delete
End Sub

Union在不同的纸张范围是不允许的,所以如果它发现的代码将失败"#REF!" 分2张。

移动:

If Not delRange Is Nothing Then delRange.Delete
Set delRange = Nothing

之前:

Next k

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM