我正在尝试编写一个宏来删除命名范围内的空白单元格(值“”,而不是空,可能包含公式),并且无法纠正运行时错误“424”,即“对象[是必需的。“
Sub DeleteBlankCellsInANamedRange()
'Deletes empty cells in a named range (shifts cells up)
For Each c In Range("myNamedRange")
If c.Value = 0 Then
myNamedRange.Cells.Delete Shift:=xlUp 'error 424 throws here
End If
Next c
MsgBox "Macro completed"
End Sub
我认为问题在于语法,并且在没有运气的情况下尝试了不同的事情。有任何想法吗?请分享。谢谢你的帮助!
答案 0 :(得分:0)
Sub DeleteBlankCellsInANamedRange()
'Deletes empty cells in a named range (shifts cells up)
Dim rngCell As Range
Dim rngDelete As Range
For Each rngCell In Range("myNamedRange").Cells
If rngCell.Value = 0 Then
If Not rngDelete Is Nothing Then
Set rngDelete = Union(rngDelete, rngCell)
Else
Set rngDelete = rngCell
End If
End If
Next
If Not rngDelete Is Nothing Then
rngDelete.Delete Shift:=xlUp
End If
MsgBox "Macro completed"
End Sub