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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,681
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
Do you have a header row & if so what row is it in?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,681
Office Version
  1. 365
Platform
  1. Windows
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
 

glen1987

New Member
Joined
Oct 4, 2019
Messages
8

ADVERTISEMENT

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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,681
Office Version
  1. 365
Platform
  1. Windows
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
 

glen1987

New Member
Joined
Oct 4, 2019
Messages
8
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,681
Office Version
  1. 365
Platform
  1. Windows
Glad to help & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,129,829
Messages
5,638,574
Members
417,037
Latest member
vedan2020

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
Top