Obtener numero de fila en listbox filtrado
Si tengo un listbox sin filtrar al hacer click con estas acciones:
'***
Private Sub ListBox2_Click()
Me.TextBox1 = Me.ListBox2.ListIndex + 1
Call cargarcampos
End Sub
'***
Private Sub cargarcampos()
Set h1 = Sheets("Sin PJ")
Set b = h1.Columns("A").Find(TextBox1.Value)
If Not b Is Nothing Then
ComboBox2.Value = h1.Cells(b.Row, 2)
TextBox4.Value = h1.Cells(b.Row, 3)
ComboBox3.Value = h1.Cells(b.Row, 4)
If h1.Cells(b.Row, 5) Then OptionButton1.Value = True Else OptionButton2 = True
ComboBox4.Value = h1.Cells(b.Row, 6)
ComboBox5.Value = h1.Cells(b.Row, 7)
ComboBox6.Value = h1.Cells(b.Row, 8)
ComboBox7.Value = h1.Cells(b.Row, 9)
TextBox5.Value = h1.Cells(b.Row, 10)
ComboBox1.Value = h1.Cells(b.Row, 11)
TextBox6.Value = h1.Cells(b.Row, 12)
TextBox7.Value = h1.Cells(b.Row, 13)
TextBox8.Value = h1.Cells(b.Row, 14)
TextBox9.Value = h1.Cells(b.Row, 15)
TextBox11.Value = h1.Cells(b.Row, 16)
TextBox10.Value = h1.Cells(b.Row, 18)
Lista = Split(h1.Cells(b.Row, 17), ",")
For i = 0 To ListBox1.ListCount - 1
Valor = ListBox1.List(i)
ListBox1.Selected(i) = False
For j = LBound(Lista) To UBound(Lista)
If Valor = Lista(j) Then
ListBox1.Selected(i) = True
End If
Next
Next
Me.CommandButton1.Enabled = False
Me.CommandButton1.Visible = False
Me.CommandButton4.Enabled = True
Me.CommandButton4.Visible = True
Me.CommandButton6.Enabled = True
Me.CommandButton6.Visible = True
Me.CommandButton9.Enabled = True
Me.CommandButton9.Visible = True
Label7.Caption = ""
Else
Label7.Caption = "No existe el id : " & TextBox1
'MsgBox "No existe el Pj : " & TextBox1
Me.TextBox1.SelStart = 6
TextBox1.SetFocus
Me.CommandButton6.Enabled = False
Me.CommandButton6.Visible = False
End If
Me.TextBox1.Enabled = False
End Sub
'***
Logro cargar los campos del form pero cuando el listbox está filtrado me pasa que si por ejemplo tengo 20 filas y si filtro por ejempo por el valor de un combo box y ese valor está en la fila 20. Al hacer click en el listbox me carga el valor de la fila 1, pues como el listbox solo tenía un valor del filtro, el index es 1 y no 20.
Así filtro el listbox:
'***
Set h2 = Sheets("Sin PJ")
ListBox2.ColumnWidths = "30 pt;200 pt;150 pt"
Me.ListBox2.Clear
For i = 2 To h2.Range("A2").CurrentRegion.Rows.Count
If CStr(h2.Cells(i, 2).Value) = CStr(Me.ComboBox2.Value) Then
'MsgBox "x: " & CStr(h2.Cells(i, 2).Value)
Me.ListBox2.AddItem Cells(i, 1)
Me.ListBox2.List(Me.ListBox2.ListCount - 1, 1) = h2.Cells(i, 2)
Me.ListBox2.List(Me.ListBox2.ListCount - 1, 2) = h2.Cells(i, 12)
Me.ListBox2.List(Me.ListBox2.ListCount - 1, 3) = h2.Cells(i, 13)
Me.ListBox2.List(Me.ListBox2.ListCount - 1, 4) = i 'se agrega la fila al listbox
End If
Next i