嗨,我有这个代码,允许删除一张纸上的单元格,之后,删除将在另一张纸上更新。这段代码一直运行良好,直到今天我再次尝试它一直给我错误
Set delRange = Union(delRange, .Cells(j, i))
。错误消息是"方法'联盟'对象_' Global'失败"
我在其他工作簿上试了一下,起初它工作了,随后又给了同样的错误。我可以知道为什么会出现此错误,是否有任何解决方案可以调试此问题?谢谢
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
答案 0 :(得分:0)
Union
个范围,因此如果代码在2个不同的工作表中找到"#REF!"
,则代码将失败。
移动:
If Not delRange Is Nothing Then delRange.Delete
Set delRange = Nothing
在:
Next k