Delete entire row if cell contains the string X

前端 未结 7 1267
感情败类
感情败类 2020-12-08 07:07

I am new to VBA and I am trying to come up with a way to delete all rows (and shift cells up, if possible) where the website column cell contains the word none.

7条回答
  •  粉色の甜心
    2020-12-08 07:20

    This was alluded to in another comment, but you could try something like this.

    Sub FilterAndDelete()
    
    Application.DisplayAlerts = False 
    
         With Sheet1 'Change this to your sheet name
    
             .AutoFilterMode = False   
             .Range("A3:K3").AutoFilter
             .Range("A3:K3").AutoFilter Field:=5, Criteria1:="none"
             .UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete 
    
         End With
    
    Application.DisplayAlerts = True
    
    End Sub
    

    I haven't tested this and it is from memory, so it may require some tweaking but it should get the job done without looping through thousands of rows. You'll need to remove the 11-Jul so that UsedRange is correct or change the offset to 2 rows instead of 1 in the .Offset(1,0).

    Generally, before I do .Delete I will run the macro with .Select instead of the Delete that way I can be sure the correct range will be deleted, that may be worth doing to check to ensure the appropriate range is being deleted.

提交回复
热议问题