2016-03-21 5 views
0

問題のコンテキストを理解するのに役立つコードスニペットは次のとおりです。 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 
+0

これは右見ていません: '.Range(.Cells(1、firstCol)、.Cells(1、firstCol)) '彼らのうちの一人が' lastCol'ではないでしょうか? – Rory

+0

この文は、firstCol = lastColのときに実行されます。その状況でいくつかの範囲オーバーラップエラーが発生しました。それ以外の条件が挿入されました。コードは実際に '.Cells(1、firstCol)'(コードの誤りが掲載されました)です。ほとんどの時間firstCol <このコンテキストではLastCol。 – sadhana

+0

1つのセル範囲で 'SpecialCells'を使うと、実際にはワークシート全体に適用されます。 – Rory

答えて

0

私はあなたが.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 
+0

空白のヘッダーを含む列を削除しています。私のコードを慎重に調べてください。 – sadhana

関連する問題