Delete entire row if cell contains the string X

前端 未结 7 1253
感情败类
感情败类 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:17
    1. Delete rows 1 and 2 so that your headings are on row 1
    2. Put this in a macro (IT WILL CHECK THROUGH ROW 75000, YOU CAN LOWER THE NUMBER IF YOU WOULD LIKE

      Columns("E:E").Select Selection.AutoFilter ActiveSheet.Range("$E$1:$E$75000").AutoFilter Field:=1, Criteria1:="none" Range("E2:E75000").SpecialCells(xlCellTypeVisible).Select Selection.EntireRow.Delete ActiveSheet.Cells.EntireRow.Hidden = False ActiveSheet.Range("$E$1:$E$75000").AutoFilter Field:=1 Columns("E:E").Select Selection.AutoFilter Range("E2").Select Range("A1").Select

    0 讨论(0)
  • 2020-12-08 07:18

    This is not necessarily a VBA task - This specific task is easiest sollowed with Auto filter.

    1.Insert Auto filter (In Excel 2010 click on home-> (Editing) Sort & Filter -> Filter)
    2. Filter on the 'Websites' column
    3. Mark the 'none' and delete them
    4. Clear filter

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-08 07:21

    Try this ...

    Dim r as Range
    Dim x as Integer
    
    For x = 5000 to 4 step -1 '---> or change as you want //Thanx 4 KazJaw
    
      set r = range("E" & format(x))
      if ucase(r.Value) = "NONE" then
        Rows(x).EntireRow.Delete
      end if 
    
    Next
    
    0 讨论(0)
  • 2020-12-08 07:27

    I'd like to add to @MBK's answer. Although I found @MBK's answer to be very helpful in solving a similar problem, it'd be better if @MBK included a screenshot of how to filter a particular column.

    0 讨论(0)
  • 2020-12-08 07:28

    In the "Developer Tab" go to "Visual Basic" and create a Module. Copy paste the following. Remember changing the code, depending on what you want. Then run the module.

      Sub sbDelete_Rows_IF_Cell_Contains_String_Text_Value()
        Dim lRow As Long
        Dim iCntr As Long
        lRow = 390
        For iCntr = lRow To 1 Step -1
            If Cells(iCntr, 5).Value = "none" Then
                Rows(iCntr).Delete
            End If
        Next
        End Sub
    

    lRow : Put the number of the rows that the current file has.

    The number "5" in the "If" is for the fifth (E) column

    0 讨论(0)
提交回复
热议问题