Utilizar la fórmula buscarv vba
Tengo un label1 que cambia de acuerdo a ciertas variables, entonce quiero utilizar la fórmula buscarv en vba para que busque lo que dice el label y lo que cuentre en la columna no de la hoja1 lo ponga en el un label2
1 Respuesta

No entendí bien tu pregunta pero supongo que quieres algo así:
Set i = Sheets("Hoja1") Set a = i.Columns("A").Find(Label1.Caption, LookAt:=xlWhole) If Not a Is Nothing Then Label2.Caption = i.Cells(a.Row, "B") End If
Lo que hace es buscar el label1 en la columna A y te regresa el valor correspondiente de la columa B en el label2.
Como no se con que evento quieres que cambie solo hay que pegarlo al final del último evento que le da valor al label1.
Suerte

Disculpe que no me se explicar bien, funciona bien, pero quiero que si en la columna E encuentra la variable "ACTIVA", traiga el valor de la columna F y si en la columna E encuentra la variable "INACTIVA", traiga el valor de la columna F

¿Esa variable esta en cualquier parte de la columna o es una celda fija?
Ya que si tienes varias celdas con la palabra activa e inactiva tendrás problemas con esa macro.

Lo que hace es buscar el label1 en la columna A y te regresa el valor correspondiente de la columna B en el label2.
Lo que quiero que haga es buscar el label1 en la columna A y si valor correspondiente en la columna E dice "ACTIVA" me regrese el valor correspondiente de la columna B en el label2
Y
Lo que quiero que haga es buscar el label1 en la columna A y si valor correspondiente en la columna E dice "INACTIVA" me regrese el valor correspondiente de la columna C en el label2

Entonces con lo que dices quedaría algo muy parecido a esto:
Set i = Sheets("Hoja1") Set a = i.Columns("A").Find(Label1.Caption, LookAt:=xlWhole) If Not a Is Nothing Then Set b = i.Cells(a.Row, "E") If b = "ACTIVA" Then Label2.Caption = i.Cells(a.Row, "B") Else Label2.Caption = i.Cells(a.Row, "C") End If End If

Entonces de acuerdo a lo que tu dices te quedaría algo así:
Set i = Sheets("Hoja1") Set a = i.Columns("A").Find(Label1.Caption, LookAt:=xlWhole) If Not a Is Nothing Then Set b = i.Cells(a.Row, "E") If b = "ACTIVA" Then Label2.Caption = i.Cells(a.Row, "B") Else Label2.Caption = i.Cells(a.Row, "C") End If End If

Este es el código con dos datos reales pero no me funciona, pensaba que lo iba a poder modificar para que funcionara pero no pude, a ver si me ayuda porque no he podido.
Gracias de antemano
Set i = Sheets("Maestro de Captaciones") Set a = i.Columns("A").Find(LabNumeroCuenta.Caption, LookAt:=xlWhole) If Not a Is Nothing Then LabeNombreClienteCuenta.Caption = i.Cells(a.Row, "B") 'nombre del cliente a label Set b = i.Cells(a.Row, "E") If b = "Ahorro" Then LabBalanceCuenta.Caption = Format(i.Cells(a.Row, "H"), "#,###.##") If b = "Ahorro" Then LabBalanceCuenta.Caption = Format(i.Cells(a.Row, "I"), "#,###.##") If b = "Ahorro" Then LabBalanceCuenta.Caption = Format(i.Cells(a.Row, "J"), "#,###.##") End If End If End If End If

Este es el que no me funciona
Set i = Sheets("Maestro de Captaciones") Set a = i.Columns("A").Find(LabNumeroCuenta.Caption, LookAt:=xlWhole) If Not a Is Nothing Then LabeNombreClienteCuenta.Caption = i.Cells(a.Row, "B") 'nombre del cliente a label Set b = i.Cells(a.Row, "E") If b = "Ahorro" Then LabBalanceCuenta.Caption = Format(i.Cells(a.Row, "H"), "#,###.##") If b = "Certificado" Then LabBalanceCuenta.Caption = Format(i.Cells(a.Row, "I"), "#,###.##") If b = "Aporte" Then LabBalanceCuenta.Caption = Format(i.Cells(a.Row, "J"), "#,###.##") End If End If End If End If

Cambiala por esta:
Set i = Sheets("Maestro de Captaciones") Set a = i.Columns("A").Find(LabNumeroCuenta.Caption, LookAt:=xlWhole) If Not a Is Nothing Then LabeNombreClienteCuenta.Caption = i.Cells(a.Row, "B") 'nombre del cliente a label Set b = i.Cells(a.Row, "E") If b = "Ahorro" Then LabBalanceCuenta.Caption = Format(i.Cells(a.Row, "H"), "#,###.##") ElseIf b = "Certificado" Then LabBalanceCuenta.Caption = Format(i.Cells(a.Row, "I"), "#,###.##") Else LabBalanceCuenta.Caption = Format(i.Cells(a.Row, "J"), "#,###.##") End If End If

Este es el código
Set i = Sheets("Maestro de Captaciones") Set a = i.Columns("A").Find(LabNumeroCuenta.Caption, LookAt:=xlWhole) If Not a Is Nothing Then LabeNombreClienteCuenta.Caption = i.Cells(a.Row, "B") 'nombre del cliente a label Set b = i.Cells(a.Row, "E") If b = "Ahorro" Then LabBalanceCuenta.Caption = Format(i.Cells(a.Row, "H"), "#,###.##") ElseIf b = "Certificado" Then LabBalanceCuenta.Caption = Format(i.Cells(a.Row, "I"), "#,###.##") ElseIf b = "Aporte" Then LabBalanceCuenta.Caption = Format(i.Cells(a.Row, "J"), "#,###.##") Else LabBalanceCuenta.Caption = "Cuenta no valida" End If End If
Pero no logro que esto función
Else LabBalanceCuenta.Caption = "Cuenta no valida"
Esto con el fin de que cuando no encuentre la cuenta diga "Cuenta no valida"
Gracias de antemano por su ayuda desinteresa y disculpe mi falta de conocimiento

Entonces queda así:
Set i = Sheets("Maestro de Captaciones") Set a = i.Columns("A").Find(LabNumeroCuenta.Caption, LookAt:=xlWhole) If a Is Nothing Then LabBalanceCuenta.Caption = "Cuenta no valida" End IF If Not a Is Nothing Then LabeNombreClienteCuenta.Caption = i.Cells(a.Row, "B") 'nombre del cliente a label Set b = i.Cells(a.Row, "E") If b = "Ahorro" Then LabBalanceCuenta.Caption = Format(i.Cells(a.Row, "H"), "#,###.##") ElseIf b = "Certificado" Then LabBalanceCuenta.Caption = Format(i.Cells(a.Row, "I"), "#,###.##") Else LabBalanceCuenta.Caption = Format(i.Cells(a.Row, "J"), "#,###.##") End If End If

No fue nada.
Como una ultima sugerencia, úsala mejor así:
Set i = Sheets("Maestro de Captaciones") Set a = i.Columns("A").Find(LabNumeroCuenta.Caption, LookAt:=xlWhole) If a Is Nothing Then MsgBox "Cuenta no valida" End IF If Not a Is Nothing Then LabeNombreClienteCuenta.Caption = i.Cells(a.Row, "B") 'nombre del cliente a label Set b = i.Cells(a.Row, "E") If b = "Ahorro" Then LabBalanceCuenta.Caption = Format(i.Cells(a.Row, "H"), "#,###.##") ElseIf b = "Certificado" Then LabBalanceCuenta.Caption = Format(i.Cells(a.Row, "I"), "#,###.##") Else LabBalanceCuenta.Caption = Format(i.Cells(a.Row, "J"), "#,###.##") End If End If
Luego hay veces que no nos fijamos en lo que dice y es mejor un mensaje que salte.
- Compartir respuesta
