Search

3

I have a problem with my application, below:

In my excel I have a sheet called parameters, in it I have two columns, where column A is the name of the parameter and column B is the value of the parameter.

I created a method called BuscarParametro where I get the parameter name (column A) and should return the value of the parameter (column B).

The method looks in column A for the value it received per parameter, finding it takes the line number and concatenates it with column B to know the value of the parameter.

My doubts would be:

  • Is there a better and more efficient way to do this?

  • In the method code, it returns nothing in the Find method, even if there is a value in the worksheet

    The following is the method below:

    Public Function BuscarParametro(Parametro As String) As String
    
        Dim Resultado As Range
        Dim Posicao As String
    
        Set Resultado = Sheets("parametros").Range("A1:A9999").Find(Parametro, _
                        LookIn:=xlValues, _
                        LookAt:=xlPart, _
                        SearchOrder:=xlByRows)
    
        If Resultado Is Nothing Then
            Exit Function
        End If
    
        Posicao = "B" & Resultado.Row
    
        BuscarParametro = Sheets("parametros").Range(Posicao)
    
    End Function
    
        
    asked by anonymous 30.05.2017 / 14:35

    1 answer

    1

    You can use the formula VLOOKUP (or #

    To find the required data on the same row in column B.

    Syntax:

    VLookup(parametro_que_quero_encontrar,
            range_que_quero_procurar, 
            retornar_valor_de_qual_coluna_do_range, 
            procurar_valor_aprox_ou_exato)
    

    See applied in code:

    Public Function BuscarParametro(param As String) As String
    
        result = Application.WorksheetFunction.VLookup _
                (param, Range("A:B"), 2, 0)
    
        BuscarParametro = result
    
    End Function
    
    Sub busca_param()
    
        MsgBox (BuscarParametro("item5"))
    
    End Sub
    

    And the result:

        
    30.05.2017 / 15:45