Search code examples
excelerasevba

Macro to read a cell value, find in a table if such value exits, if it does, delete table row and shift up


Forgot the code:

It returns object variable error. I tried to record a macro but Find doesnt function properly with copy and paste, and then, macro records the actual row I´m in, not a variable.

Instead of Find, I also tried " Cells.AutoFilter Field:=2, Criteria1:=x" but that would return Autofilter range class failed. I´m stuck. Hope it helps.

Sub alta()
'
' alta Macro
x = Range("I3").Select
Selection.Copy
Selection.Find(What:=x, After:=ActiveCell, LookIn:=xlValues, LookAt _
    :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    True, SearchFormat:=False).Activate
Application.CutCopyMode = False
Selection.EntireRow.Delete
Range("I3").Select
Selection.ClearContents
End Sub

Solution

  • This should get you started:

    Private Sub CommandButton1_Click()
        If Range("I3").Value <> "" Then
            If IsError(Application.Match(Range("I3"), Range("B3:B20"), 0)) Then
                MsgBox ("No Match")
            Else
                foundRow = Application.Match(Range("I3"), Range("B3:B20"), 0) + 2
    
                Range("A" & foundRow & ":B" & foundRow).Delete Shift:=xlUp
            End If
        End If
    End Sub