excelvbarow

How to find a value and get the row number in VBA


I'm trying to get the row which contains a specific value.

For example, I've an ID for a client. I want to modify or remove the info of that client on the table.
I'm trying to get the number of the row where that ID is located and change the cells of that "value" but I can't reach that number.

This returns nothing

valor_buscado = Me.Codigo_txt
Set Fila = Sheets("Clientes").Range("A:A").Find(valor_buscado , lookat:=xlWhole)

Solution

  • Try this out:

    Private Sub Modify_Click()
    
        Dim FILA As Range
        Dim codigo As Long, valor_buscado, rw As Range
        
        'codigo = CLng(Codigo_txt.value)'???
        valor_buscado = Trim(Me.Codigo_txt) 'in case of leading/trailing spaces
        
        Set FILA = Sheets("Clientes").Range("A:A").Find(valor_buscado, _
                                lookat:=xlWhole, LookIn:=xlValues)
        If Not FILA Is Nothing Then
            With FILA.EntireRow
                .Columns("B").value = Me.txt_name.value
                'etc etc
                .Columns("I").value = Me.txt_email.value
            End With
        Else
            MsgBox "Value '" & valor_buscado & "' was not found!" '
        End If
    
    End Sub