簡體   English   中英

使用specialcells屬性時出現1004應用程序定義或對象定義的錯誤

[英]1004 application defined or object defined error while using specialcells property

以下是有助於理解問題背景的代碼段。 如果Not(r為空)條件為true,則我收到1004錯誤。 奇怪的是,如果我一步一步地在調試模式下運行代碼,則每當第二次滿足條件時就會發生錯誤。 請幫助我解決此錯誤。 謝謝。

With wb.Sheets(csht)
    lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    firstCol = 1
    'If IsEmpty(.Cells(1, 1).Value) Then firstCol = .Cells(1, 1).End(xlToRight).Column
End With

'countCol = lastCol - firstCol + 1
On Error Resume Next
If firstCol < lastCol Then
    Set r = wb.Sheets(csht).Range(wb.Sheets(csht).Cells(1, firstCol), wb.Sheets(csht).Cells(1, lastCol)).SpecialCells(xlCellTypeBlanks)
Else
    Set r = wb.Sheets(csht).Range(wb.Sheets(csht).Cells(1, firstCol), wb.Sheets(csht).Cells(1, firstCol)).SpecialCells(xlCellTypeBlanks)
End If
Err.Clear
On Error GoTo 0

If Not (r Is Nothing) Then
    'On Error Resume Next
    With wb.Sheets(csht)
        If firstCol < lastCol Then
            .Range(.Cells(1, firstCol), .Cells(1, lastCol)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
        Else
            .Range(.Cells(1, firstCol), .Cells(1, firstCol)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
        End If
    End With
'Err.Clear
'On Error GoTo 0
End If

我認為您根本不需要.SpecialCells(xlCellTypeBlanks)件。 由於您只是刪除整個列,因此可以將代碼更改為此:

With wb.Sheets(csht)
If firstCol < lastCol Then
        .Range(.Cells(1, firstCol), .Cells(1, lastCol)).EntireColumn.Delete
    Else
        .Range(.Cells(1, firstCol), .Cells(1, firstCol)).EntireColumn.Delete
    End If
End With

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM