[英]“No Cells Found Error” in Excel vba
我使用以下代碼來取消並復制單元格。 這是我正在使用的代碼。
Sub unMerge()
Dim lastRow As Long
Dim lastCol As Long
lastRow = Range("B2").End(xlDown).Row
lastCol = Range("A2").End(xlToRight).Column
For iCol = 1 To lastCol
Columns(iCol).unMerge
Columns(iCol).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=r[-1]c"
Next iCol
End Sub
當列中有合並的單元格時,代碼可以順利運行,但是當遇到沒有合並單元格的列時,它會給出標題錯誤。 什么可能是代碼中的錯誤。
如果未找到空白單元格,則SpecialCells方法將出錯。 為避免這種情況,您可以使用簡單的錯誤處理來跳過該錯誤
On Error Resume Next
Columns(iCol).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=r[-1]c"
如果其他一切順利運行,這是解決它的好方法:
Sub unMerge()
Dim lastRow As Long
Dim lastCol As Long
Dim iCol As Long
lastRow = Range("B2").End(xlDown).Row
lastCol = Range("A2").End(xlToRight).Column
For iCol = 1 To lastCol
If Columns(iCol).MergeCells Then Columns(iCol).unMerge
If RangeContainsCellTypeBlanks(Columns(iCol)) Then
Columns(iCol).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=r[-1]c"
End If
Next iCol
End Sub
Public Function RangeContainsCellTypeBlanks(rng As Range) As Boolean
On Error GoTo RangeContainsCellTypeBlanks_Error
If rng.Cells.SpecialCells(xlCellTypeBlanks).Count > 0 Then RangeContainsCellTypeBlanks = True
On Error GoTo 0
Exit Function
RangeContainsCellTypeBlanks_Error:
RangeContainsCellTypeBlanks = False
End Function
它檢查合並的單元格,如果找到它們,則執行unMerge並寫入FormulaR1C1。 以下是MergeCells
屬性的Microsoft文檔: https : MergeCells
關於SpecialCellsTypeBlanks,顯然有一個已知的限制,不允許輕松繞過它,因此應該使用On Error Resume Next
,雖然我真的不喜歡這個錯誤捕獲 - https://www.rondebruin .NL / WIN / S4 / win003.htm
因此,至少我在布爾函數中使用它,確保它不會污染其余的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.