Results 1 to 6 of 6

Thread: Based on Cell Criteria Delete Row in excel

  1. #1
    Join Date
    Jun 2011
    Posts
    109

    Based on Cell Criteria Delete Row in excel

    My question is that I want to delete a row. Delete Row based on Cell Criteria. The function should be similar to as follows:

    • Delete the corresponding row in the first column with the number, if cell A1 = 3.


    • An for instance:

      Cell a1 contains a number with random output by that of a command button.


    Also I have column through 74 that is numbered 1. The Row has to be deleted if the number in cell a1 matches that of a row. Your help will be appreciated.

  2. #2
    Join Date
    May 2009
    Posts
    529

    re: Based on Cell Criteria Delete Row in excel

    Just copy paste the below code in your actual source code. The output will be as per your requirement. The code based on criteria input works to delete a row.


    Code:
    Range("a1:a74").Select
        For Each cl In Selection
            If cl.Text = ComboBox1.Text Then
                cl.EntireRow.Delete
            End If
        Next
    Hope you get the appropriate help from the code.

  3. #3
    Join Date
    May 2009
    Posts
    543

    re: Based on Cell Criteria Delete Row in excel

    I have modified the code more precisely. Use it in your actual source code. Before that refer to it and make some minor changes as per your code. This code is from a webpage with success.


    Code:
     Dim DeleteValue As String
        Dim rng As Range    
    
    DeleteValue = ("Randomizer!d3")
        ' This will delete the rows with "ron" in the Range("A1:A100")
    
        With ActiveSheet
            Sheet9.Range("A1:A100").AutoFilter Field:=1, Criteria1:=DeleteValue
            With ActiveSheet.AutoFilter.Range
                On Error Resume Next
                Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
                          .SpecialCells(xlCellTypeVisible)
                On Error GoTo 0
                If Not rng Is Nothing Then rng.EntireRow.Delete
    
            End With
            .AutoFilterMode = False
        End With

  4. #4
    Join Date
    May 2009
    Posts
    511

    re: Based on Cell Criteria Delete Row in excel

    If you haven’t found help from the above code than try this. The syntax which I have used is very easy and understandable whenever you refer. I hope you won’t have any question on it . Just take all necessary help from it.

    Code:
    Sub test()
        Dim maxRow As Long
        Dim testVal As Double
        Dim i As Long
        
        Rem find last row in SBS Col A
    maxRow = Sheets("SBS").Range("a65536").End(xlUp)
        Rem get random value from Randomizer
    testVal = Sheets("Randomizer").Range("a1").Value
        
    For i = maxRow To 1 Step -1
        If Sheets("SBS").Cells(i, 1) = testVal Then
            Sheets("SBS").Cells(i, 1).EntireRow.Delete shift:=xlUp
            Exit For
        End If
    Next i
    
    End Sub

  5. #5
    Join Date
    May 2009
    Posts
    637

    re: Based on Cell Criteria Delete Row in excel

    Also add the below code in your actual source code so that you can set the range of selection. While taking a look on the code you will understand by yourself.

    Code:
    Range("a1:a74").Select
        For Each cl In Selection
            If cl.Text = ComboBox1.Text Then
                cl.EntireRow.Delete
            End If
        Next

  6. #6
    Join Date
    May 2009
    Posts
    527

    re: Based on Cell Criteria Delete Row in excel

    I would like to add on it. The above code is incomplete so you can’t directly copy paste on the main source code. Just see the below code and see the difference. The above code is excellent but its bit incomplete. Its necessary that you have to mention the string syntax.

    Code:
    Dim DeleteValue As String
        Dim rng As Range    
    
    DeleteValue = ("Randomizer!d3")
        ' This will delete the rows with "ron" in the Range("A1:A100")
    
        With ActiveSheet
            Sheet9.Range("A1:A100").AutoFilter Field:=1, Criteria1:=DeleteValue
            With ActiveSheet.AutoFilter.Range
    
    Range("a1:a74").Select
        For Each cl In Selection
            If cl.Text = ComboBox1.Text Then
                cl.EntireRow.Delete
            End If
        Next

Similar Threads

  1. How to delete first 4 letters of Cell in Microsoft Excel
    By Ulrich in forum MS Office Support
    Replies: 2
    Last Post: 18-01-2012, 06:51 PM
  2. How to change cell color in excel based on text input
    By Chini mao in forum Windows Software
    Replies: 2
    Last Post: 06-01-2012, 09:19 PM
  3. excel: delete (or find) cell if it meets certain criteria
    By cmendes in forum Windows Software
    Replies: 3
    Last Post: 12-11-2011, 12:14 PM
  4. insert picture in excel 2007 based on cell reference
    By joe.polkendare in forum Windows Software
    Replies: 5
    Last Post: 25-07-2011, 11:40 PM
  5. Replies: 6
    Last Post: 23-07-2011, 01:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,568,544.25425 seconds with 17 queries