2016-09-14 17 views
0

私はVBAの新しいプログラマです。私はfor-loopから行を削除するために単純な行を使用しています。この単純な行は、現在の行を削除します。VBAを使用してExcel内の行を削除する

Rows(i).EntireRow.Delete 

ただし、削除したくないコンテンツはD列にあります。 "EntireRow"を "A、B、C"のようなものに正当化する方法はありますか?

a b c d 
1 x x x N 
2 x x x N 
3 x x x N 

X =必要ありません。 N =必要とされる。

+0

'Range(" A "&i:" C "&i)' –

答えて

6

ますマクロの記録場合は、あなたが何かを得る:ので、ここで

Selection.Delete Shift:=xlUp 

は、いくつかのオプションがあります

Rows(i).Resize(, 3).Delete xlUp 

Range("A:C").Rows(i).Delete xlUp 

Range("A1:C1").Offset(i - 1).Delete xlUp 
0

は、このVBAコードを試してみてください。

Sub Delete_Row_If_Not_Whatever() 
Dim RowToTest As Long 
'Macro deletes entire row if "Whatever" is not found in column "D" 

Application.ScreenUpdating = False 'Turn off screen updating. Code runs faster without screen flicker 

'Column Count 4 = "D" 
For RowToTest = Cells(Rows.Count, 4).End(xlUp).row To 2 Step -1 

'Look for "Whatever" save row and delete other rows. Replace "Whatever" with what you want to keep. 
'Column Count 4 = "D" 
With Cells(RowToTest, 4) 
    If .Value <> "Whatever" _ 
    Then _ 
    Rows(RowToTest).EntireRow.Delete 
End With 

Next RowToTest 

Application.ScreenUpdating = True 'Turn on screen updating 
End Sub 

は必ず保存してくださいバックアップを開始する前に。 :)

関連する問題