Vba Delete row if data from another cell exists

glen1987

New Member
Joined
Oct 4, 2019
Messages
8
Hi,

I have the below code however I need to change it to reference cells C4-C4 and delete the row, if Column H contains the data typed in cells C4-C7.

Thank you for any help

.
Code:
Sub Loop_Example()
    Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long

    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With
    
    With ActiveSheet

  
        .Select

        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView

        'Turn off Page Breaks, we do this for speed
        .DisplayPageBreaks = False

        'Set the first and last row to loop through
        Firstrow = .UsedRange.Cells(1).Row
        Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

        'We loop from Lastrow to Firstrow (bottom to top)
        For Lrow = Lastrow To Firstrow Step -1

            'We check the values in the A column in this example
            With .Cells(Lrow, "H")

                If Not IsError(.Value) Then

                    If .Value = "Data" Then .EntireRow.Delete
                    'This will delete each row with the Value "Data"                    'in Column , case sensitive.

                End If

            End With

        Next Lrow

    End With

    ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With

End Sub
 
Last edited by a moderator:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi & welcome to MrExcel.
Do you have a header row & if so what row is it in?
 
Upvote 0
Ok, how about
Code:
Sub Glen1987()
   Dim Ary As Variant
   
   Ary = Application.Transpose(Range("C4:C7").Value2)
   With ActiveSheet
      .Range("A9:H9").AutoFilter 8, Ary, xlFilterValues
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Ok, how about
Code:
Sub Glen1987()
   Dim Ary As Variant
   
   Ary = Application.Transpose(Range("C4:C7").Value2)
   With ActiveSheet
      .Range("A9:H9").AutoFilter 8, Ary, xlFilterValues
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub

Hi Fluff, the code works perfectly when typing names etc however when I use numbers I doesn't work.
Thank you for your help
 
Upvote 0
Ok, how about
Rich (BB code):
Sub Glen1987()
   Dim ary(1 To 4) As String
   Dim i As Long
   
   For i = 1 To 4
      ary(i) = Cells(i + 3, 3)
   Next i
   With ActiveSheet
      .Range("A9:H9").AutoFilter 8, ary, xlFilterValues
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Ok, how about
Rich (BB code):
Sub Glen1987()
   Dim ary(1 To 4) As String
   Dim i As Long
   
   For i = 1 To 4
      ary(i) = Cells(i + 3, 3)
   Next i
   With ActiveSheet
      .Range("A9:H9").AutoFilter 8, ary, xlFilterValues
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub

It works perfectly thank you for your help
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,684
Members
448,977
Latest member
dbonilla0331

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top