問題のコンテキストを理解するのに役立つコードスニペットは次のとおりです。 Not(rは何もない)条件が真であれば1004エラーが発生します。私はデバッグモードでコードをステップバイステップで実行すると、条件が満たされるたびにエラーが発生します。このエラーを修正するのを助けてください。ありがとう。specialcellsプロパティを使用中にアプリケーション定義またはオブジェクト定義のエラーが発生しました
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
これは右見ていません: '.Range(.Cells(1、firstCol)、.Cells(1、firstCol)) '彼らのうちの一人が' lastCol'ではないでしょうか? – Rory
この文は、firstCol = lastColのときに実行されます。その状況でいくつかの範囲オーバーラップエラーが発生しました。それ以外の条件が挿入されました。コードは実際に '.Cells(1、firstCol)'(コードの誤りが掲載されました)です。ほとんどの時間firstCol <このコンテキストではLastCol。 – sadhana
1つのセル範囲で 'SpecialCells'を使うと、実際にはワークシート全体に適用されます。 – Rory