Pasar esta formula a VBA o formula para realizar busqueda

Anónimas
2023-03-03T13:08:30+00:00

Buen dia comunidad estoy realizando una aplicacion en la cual a traves de un userform

voy a a registrar ciertos productos e informacion y en estos momentos necesito realizar el boton

de busqueda el cual tomara de las celdas los datos y mostrarlas en unos labels y text box,

no se requiere extraer todos los registros que tiene si no el ultimo realizado.

Para ello probe con el siguiente codigo.

On Error GoTo ManejadorErrores

            lb\_Date = Application.WorksheetFunction.Index(Sheets("CX50").Range("A:A"), Application.WorksheetFunction.Match(TB\_SN, Sheets("CX50").Range("E:E"))) 

            CB\_Model = Application.WorksheetFunction.Index(Sheets("CX50").Range("B:B"), Application.WorksheetFunction.Match(TB\_SN, Sheets("CX50").Range("E:E"))) 

            TB\_PNin = Application.WorksheetFunction.Index(Sheets("CX50").Range("C:C"), Application.WorksheetFunction.Match(TB\_SN, Sheets("CX50").Range("E:E"))) 

            TB\_PNout = Application.WorksheetFunction.Index(Sheets("CX50").Range("D:D"), Application.WorksheetFunction.Match(TB\_SN, Sheets("CX50").Range("E:E"))) 

            TB\_WO = Application.WorksheetFunction.Index(Sheets("CX50").Range("F:F"), Application.WorksheetFunction.Match(TB\_SN, Sheets("CX50").Range("E:E"))) 

            TB\_Description = Application.WorksheetFunction.Index(Sheets("CX50").Range("G:G"), Application.WorksheetFunction.Match(TB\_SN, Sheets("CX50").Range("E:E"))) 

            TB\_Comment = Application.WorksheetFunction.Index(Sheets("CX50").Range("H:H"), Application.WorksheetFunction.Match(TB\_SN, Sheets("CX50").Range("E:E"))) 

            CB\_FM = Application.WorksheetFunction.Index(Sheets("CX50").Range("I:I"), Application.WorksheetFunction.Match(TB\_SN, Sheets("CX50").Range("E:E"))) 

            TB\_FMS = Application.WorksheetFunction.Index(Sheets("CX50").Range("J:J"), Application.WorksheetFunction.Match(TB\_SN, Sheets("CX50").Range("E:E"))) 

            CB\_From = Application.WorksheetFunction.Index(Sheets("CX50").Range("K:K"), Application.WorksheetFunction.Match(TB\_SN, Sheets("CX50").Range("E:E"))) 

            CB\_Tech = Application.WorksheetFunction.Index(Sheets("CX50").Range("L:L"), Application.WorksheetFunction.Match(TB\_SN, Sheets("CX50").Range("E:E"))) 

            Lb\_Shift = Application.WorksheetFunction.Index(Sheets("CX50").Range("M:M"), Application.WorksheetFunction.Match(TB\_SN, Sheets("CX50").Range("E:E"))) 

            CB\_Cloud = Application.WorksheetFunction.Index(Sheets("CX50").Range("N:N"), Application.WorksheetFunction.Match(TB\_SN, Sheets("CX50").Range("E:E"))) 

            CB\_Status = Application.WorksheetFunction.Index(Sheets("CX50").Range("O:O"), Application.WorksheetFunction.Match(TB\_SN, Sheets("CX50").Range("E:E"))) 

            CB\_RPRby = Application.WorksheetFunction.Index(Sheets("CX50").Range("P:P"), Application.WorksheetFunction.Match(TB\_SN, Sheets("CX50").Range("E:E"))) 

            TB\_SNsub = Application.WorksheetFunction.Index(Sheets("CX50").Range("Q:Q"), Application.WorksheetFunction.Match(TB\_SN, Sheets("CX50").Range("E:E"))) 

            TB\_PNsub = Application.WorksheetFunction.Index(Sheets("CX50").Range("R:R"), Application.WorksheetFunction.Match(TB\_SN, Sheets("CX50").Range("E:E"))) 

            Boton\_Guardar.Enabled = False

El problema de este codigo es que no me muestra el ultimo registro aunque si extrae los datos.

En la hoja de calculo que tengo implemente esta formula

=INDEX(A2:A5,MAX(IF(IFERROR(MATCH(F2:F5,H12,0),"")=1,ROW(F2:F5)))) y esta si me da el ultimo registro realizado. El problema es que tendria que pasarlo a vba

o usar una hoja de calculo alterna solo para esta funcion, la segunda opcion no se me hace viable esto debido a que mi documento se me haria mas grande y tendria que vincular las hojas a dicha pagina trato de que sea lo mas auutomatizado posible pero cuando trato de hacer esta misma funcion en vba por la palabra reservada if no me aparece y no la puedo tener, hay alguna sugerencia que me puedan dar. Como dije y reitero necesito que esta funcion solo extraiga el último dato registrado.

Microsoft 365 y Office | Excel | Otros | Windows

Pregunta bloqueada. Esta pregunta se migró desde la Comunidad de Soporte técnico de Microsoft. Puede votar si es útil, pero no puede agregar comentarios o respuestas ni seguir la pregunta.

0 comentarios No hay comentarios

18 respuestas

Ordenar por: Más antiguo
  1. Anónimas
    2023-03-04T23:29:33+00:00

    ... no puedo usar filtrado porque hay gente que utiliza el excel al mismo tiempo y ...

    Tus palabras me indican que no has probado mi ejemplo (yo no he hablado de filtrar) y también que no has entendido lo de «... resuelve mejor así (sin alterar los datos originales)...».

    Para no seguir con un diálogo de sordos, mi participación acaba aquí.

    ¿Le ha resultado útil esta respuesta?

    0 comentarios No hay comentarios
  2. Héctor Miguel 71,520 Puntos de reputación
    2023-03-05T23:54:19+00:00

    ... este software no va a quedar en este lenguaje, se modificará a C# ...

    eso de que lo vas a pasar a C# no lo habias comentado (?)

    asumo que tienes (o conoces quien tenga) los recursos suficientes y necesarios para la conversion (?)

    en via de mientras y para los primeros 7 controles (en tu codigo de muestra):

    lb_Date = Application.WorksheetFunction.Index(Sheets("CX50").Range("A:A"), Application.WorksheetFunction.Match(TB_SN, Sheets("CX50").Range("E:E"))) 
    
    
    
    CB_Model = Application.WorksheetFunction.Index(Sheets("CX50").Range("B:B"), Application.WorksheetFunction.Match(TB_SN, Sheets("CX50").Range("E:E")))  
    
    
    
    TB_PNin = Application.WorksheetFunction.Index(Sheets("CX50").Range("C:C"), Application.WorksheetFunction.Match(TB_SN, Sheets("CX50").Range("E:E")))  
    
    
    
    TB_PNout = Application.WorksheetFunction.Index(Sheets("CX50").Range("D:D"), Application.WorksheetFunction.Match(TB_SN, Sheets("CX50").Range("E:E")))  
    
    
    
    TB_WO = Application.WorksheetFunction.Index(Sheets("CX50").Range("F:F"), Application.WorksheetFunction.Match(TB_SN, Sheets("CX50").Range("E:E")))  
    
    
    
    TB_Description = Application.WorksheetFunction.Index(Sheets("CX50").Range("G:G"), Application.WorksheetFunction.Match(TB_SN, Sheets("CX50").Range("E:E")))  
    
    
    
    TB_Comment = Application.WorksheetFunction.Index(Sheets("CX50").Range("H:H"), Application.WorksheetFunction.Match(TB_SN, Sheets("CX50").Range("E:E")))
    

    esto suponiendo que ese codigo lo tienes en el modulo del formulario, en el evento '_click' de un "commandbutton1"

    si la realidad es diferente (poque te comiste los detalles)... modifica/adapta donde y como corresponda

    ' pon en ambas constantes el mismo numero de elementos separados por un espacio ' 
    
    Private Const cols As String = "a b c d f g h" ' <= OJO con las columnas a omitir ' 
    
    Private Const ctrls As String = "lb_date cb_model tb_pnin tb_pnout tb_wo tb_description tb_comment" 
    
    Private Sub CommandButton1_Click() 
    
      Dim col, ctrl, t As String, b As String, f As Long, n As Long 
    
      col = Split(cols) 
    
      ctrl = Split(ctrls) 
    
      t = IIf(Val(TB_SN) > 0, "", """") 
    
      With Worksheets("cx50") 
    
        b = .Range("e:e").Address(, , , 1) 
    
        If WorksheetFunction.CountIf(.Range("e:e"), TB_SN) = 0 Then MsgBox "sin datos coincidentes !": Exit Sub 
    
        f = Evaluate("lookup(2,1/(" & b & " = " & t & TB_SN & t & "),row(" & b & "))") 
    
        For n = LBound(col) To UBound(col): Controls(ctrl(n)) = .Range(col(n) & f): Next 
    
      End With 
    
    End Sub
    

    ¿Le ha resultado útil esta respuesta?

    0 comentarios No hay comentarios
  3. Anónimas
    2023-03-06T01:14:26+00:00

    Si es en un evento click el recurso es que mostrando este user form y una variante vean el porque es mejor ya que ahorita se manejan en puro excel pero por el flujo de información puede que excel se relentice y se crashee por tanta información almacenada y la intención es mirarlo a C# y SQL.

    Y probaré con ese código haber si funciona.

    ¿Le ha resultado útil esta respuesta?

    0 comentarios No hay comentarios