ListView is incoherent after a filter

jqfranco

New Member
Joined
Dec 27, 2017
Messages
28
Good Morning
After making a data filter in the Listview, I see that the filter is carried out in the data base sheet and that the Listview shows only the filtered lines.
But when choosing a row in the Listview, the Textbox is filled with data from another row. As if the filter hadn't been done,
How can I resolve this situation? Thank you for your help.


VBA Code:
Private Sub CASAS_LView_Casas_ItemClick(ByVal Item As MSComctlLib.ListItem)
    Dim v_Contador As Integer
    Dim v_Ultlinha As Integer
    'In listview the first line is 1 but in BD the first line is 3
    v_LinhaList = Item.Index + 2                 
    v_Ultlinha = Folha20.ListObjects("BDR_Casas").DataBodyRange.Rows.Count
     
    For v_Contador = 1 To v_Ultlinha
        Form_01_Entrada.MultiPage1.Pages(2).CBox_Casas_Obras.Value = Folha20.Cells(v_LinhaList, 2) 
        Form_01_Entrada.MultiPage1.Pages(2).Casas_TextBox3.Value = Folha20.Cells(v_LinhaList, 3) 
        Form_01_Entrada.MultiPage1.Pages(2).CBox_Casas_CasasDesc.Value = Folha20.Cells(v_LinhaList, 4) 
    Next
End Sub

'______________________________________________________________________________________________________

Private Sub CASAS_ComboBox_P02_Casaschange()
    Dim v_MPage2 As Variant
    Set v_MPage2 = Form_01_Entrada.MultiPage1.Pages(2)
    If v_MPage2.ComboBox_P02_Casas.Value = "Codigo" Then
        Folha20.ListObjects("BDR_Casas").Range.AutoFilter Field:=2, Criteria1:="=*" & v_MPage2.TextBox_P02_Casas & "*", Operator:=xlAnd
        Call CASAS_Listview_Create
    End If
    If v_MPage2.ComboBox_P02_Casas.Value = "Obra" Then
        Folha20.ListObjects("BDR_Casas").Range.AutoFilter Field:=3, Criteria1:="=*" & v_MPage2.TextBox_P02_Casas & "*", Operator:=xlAnd
        Call CASAS_Listview_Create
    End If
    If v_MPage2.ComboBox_P02_Casas.Value = "Casa" Then
        Folha20.ListObjects("BDR_Casas").Range.AutoFilter Field:=4, Criteria1:="=*" & v_MPage2.TextBox_P02_Casas & "*", Operator:=xlAnd
        Call CASAS_Listview_Create
    End If
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Rich (BB code):
Private Sub CASAS_LView_Casas_ItemClick(ByVal Item As MSComctlLib.ListItem)
    Dim v_Contador As Integer
    Dim v_Ultlinha As Integer
   
    'In listview the first line is 1 but in BD the first line is 3

If the data is filtered, then the first row in the listview will not necessarily be the third in the database.
Therefore in this instruction we must find the row in the database that corresponds to the row selected in the listview.
    
    v_LinhaList = Item.Index + 2                 

    v_Ultlinha = Folha20.ListObjects("BDR_Casas").DataBodyRange.Rows.Count
   
The cycle is not necessary.
    For v_Contador = 1 To v_Ultlinha
        Form_01_Entrada.MultiPage1.Pages(2).CBox_Casas_Obras.Value = Folha20.Cells(v_LinhaList, 2)
        Form_01_Entrada.MultiPage1.Pages(2).Casas_TextBox3.Value = Folha20.Cells(v_LinhaList, 3)
        Form_01_Entrada.MultiPage1.Pages(2).CBox_Casas_CasasDesc.Value = Folha20.Cells(v_LinhaList, 4)
    Next
End Sub

To fill the textbox, I show you several options:
1. The simplest, if the data is in the listview record, only pass the data from the listview field to the textbox.
2. If the data is not in listview, you can load the data in listview, so that at this point you can use it.
3. Search the row in the database with a key, that is, if you have a column that has unique values, then locate that unique value, and with that you have the row on the sheet.
4. Add the row number to the listview when filling the listview.

If you are in doubt about how to apply any of the options, then you can share your file.

You could upload a copy of your file to a free site such www.dropbox.com or google drive. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,361
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top