2011-10-18 17 views
0

私はさまざまなエラーを取り出し、エラーコードの説明をデータに割り当てるために次のコードブロックを用意しています。フィルタが結果を返す限り正常に動作します。そうでなければ、ヘッダー行を削除します。それをどうやって防ぐことができますか?前もって感謝します。Excel VBA - 動的フィルタ範囲の削除

Sheets("Tempsheet").Select 
Range("A1:K1").AutoFilter 
Range("A1:K1").AutoFilter Field:=5, Criteria1:="0", Criteria2:=0 
Range("K2:K" & Range("A" & Rows.Count).End(xlUp).Row).Formula = "Excluded: $0.00 Amount" 
Range("A2:K" & Range("A" & Rows.Count).End(xlUp).Row).EntireRow.Copy 
Sheets("Excluded").Select 
Range("A2").PasteSpecial 
Sheets("Tempsheet").Select 
Range("A2:K" & Range("A" & Rows.Count).End(xlUp).Row).EntireRow.Delete 
Sheets("Tempsheet").AutoFilterMode = False 
+0

私が必要とまさに、すべての答えをありがとう! – kwilmarth

+0

問題ありません。 SpecialCellsアプローチは、データレイアウトに関係なく機能します(つまり、行1または行10ではなく行10で始まった場合)、現在のレイアウトを超える柔軟性を提供します。 – brettdj

答えて

3

何もデータがフィルタによって返されない場合、その後Range("A2:K" & Range("A" & Rows.Count).End(xlUp).Row)は、フィルタ結果をテストこのコードのように削除

If Range("A2:K" & Range("A" & Rows.Count).End(xlUp).Row).Row > 1 then 
    ... .Delete 
End If 
1

何かがそれを行う必要があります実行する前に、行1、そうrow > 1ためのテストを返します。

Dim ws As Worksheet 
Dim ws2 As Worksheet 
Set ws = Sheets("Tempsheet") 
Set ws2 = Sheets("Excluded") 
Set rng1 = ws.Range(ws.[a1], ws.Cells(Rows.Count, "k").End(xlUp)) 
rng1.AutoFilter Field:=5, Criteria1:="0", Criteria2:=0 
If rng1.SpecialCells(xlVisible).Rows.Count > 1 Then 
    ws.Range("K2:K" & Range("A" & Rows.Count).End(xlUp).Row).Formula = "Excluded: $0.00 Amount" 
    ws.Range("A2:K" & Range("A" & Rows.Count).End(xlUp).Row).EntireRow.Copy 
    ws2.[a2].PasteSpecial Paste:=xlPasteValues 
    rng1.Offset(1, 0).Resize(rng1.SpecialCells(xlVisible).Rows.Count - 1).EntireRow.Delete 
End If 
Sheets("Tempsheet").AutoFilterMode = False 
0
Sheets("Tempsheet").Select 
Range("A1:K1").AutoFilter 
Range("A1:K1").AutoFilter Field:=5, Criteria1:="0", Criteria2:=0 
Range("K2:K" & Range("A" & Rows.Count).End(xlUp).Row).Formula = "Excluded: $0.00 Amount" 
Range("A2:K" & Range("A" & Rows.Count).End(xlUp).Row).EntireRow.Copy 
Sheets("Excluded").Select 
Range("A2").PasteSpecial 
Sheets("Tempsheet").Select 

if Range("A" & Rows.Count).End(xlUp).Row > 1 then 
    Range("A2:K" & Range("A" & Rows.Count).End(xlUp).Row).EntireRow.Delete 
end if 

Sheets("Tempsheet").AutoFilterMode = False 
関連する問題