Find and Delete sub

arjking

New Member
Joined
May 23, 2008
Messages
11
I'm writing a sub that compares two tables on the same sheet. It loops through the first table, checks if a cell in a certain column is blank. If so, it should go to the second table and find and delete each row in the second table that shares a field value "strState" with the observation in the first table.

Code:
Private Sub RemoveEntries()
  Dim lngLoop As Long 'loop variable
  Dim lngDeleteRow As Long 'the row about to be deleted
  Dim strState As String 'the string that I get from table 1 and I need to find in table 2
 
  With Workbooks(wbFutureName).Sheets(wbImportName)
  For lngLoop = 3 To LastRow
    If .Range("I" & lngLoop).Value = "" Then
      strState = .Range("B" & lngLoop).Value
      Do
        Set lngDeleteRow = .Range("DB3:DB" & LastRow).Find(What:=strState, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row
        .Range("DA" & lDeleteRow & ":DP" & lDeleteRow).Delete (xlShiftUp)
 
      Loop While Not .Range("DB3:DB" & LastRow).Find(What:=strState, After:=.Range("DB3"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious) Is Nothing
    End If
  Next lngLoop
  End With
End Sub


I get Compile Error: Object Required


I'm trying to model the code after:

Code:
Sub FindLastCell()
Workbooks(wbFutureName).Sheets(wbImportName).Activate
If WorksheetFunction.CountA(Cells) > 0 Then
  'Search for any entry, by searching backwards by Rows.
  LastRow = Cells.Find(What:="*", After:=[A1], _
  SearchOrder:=xlByRows, _
  SearchDirection:=xlPrevious).row
  'Search for any entry, by searching backwards by Columns.
  LastColumn = Cells.Find(What:="*", After:=[A1], _
  SearchOrder:=xlByColumns, _
  SearchDirection:=xlPrevious).Column
  'MsgBox Cells(LastRow, LastColumn).Address
End If
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Deleting the 'Set' in

Set lngDeleteRow = .Range("DB3:DB" & LastRow).Find(What:=strState, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row

will fix the compile error.
 
Upvote 0
Thank you for the quick reply.

If I take away the "Set" command then I get Run-time error 91, Object Variable or With Block variable not set....
 
Upvote 0
Replace your Do Loop with:
Code:
      Do
        Dim rFind As Range
        Set rFind = .Range("DB3:DB" & lastrow).Find(What:=strState, SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
        If Not rFind Is Nothing Then
            lngDeleteRow = rFind.Row
            .Range("DA" & lngDeleteRow & ":DP" & lngDeleteRow).Delete (xlShiftUp)
        End If
      Loop Until rFind Is Nothing
Also, always put Option Explicit at the top of every module. Tools - Options - Require variable declaration will put it there for you automatically. This would catch the undeclared variable lDeleteRow.
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,500
Members
449,090
Latest member
RandomExceller01

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