New Excel Forum

This forum has been moved to TeachExcel.com

Ask all future questions in the New Excel Forum.

ExcelKey

[SOLVED] ListView does not filter

Macros, VBA, Excel Automation, etc.

[SOLVED] ListView does not filter

Postby marreco » Tue Jan 24, 2012 4:17 am

Hi...

Hello
I would like your help to set a filter in my listview.

the first is in txtPesquisa, like when I type a number, it was filtered into the listview, column 4 only.


the second and TextBox2, I would enter a company name, column 2 of my worksheet "Sheet1".

In this second part generates an error "6"
  • 0

You do not have the required permissions to view the files attached to this post.
Last edited by marreco on Tue Jan 24, 2012 6:52 pm, edited 1 time in total.
marreco
Regular
 
Posts: 51
Joined: Jan 13, 2012
Reputation: 0

Re: ListView does not filter

Postby Sisyphus » Tue Jan 24, 2012 6:24 am

Hi,
20 minutes of programming can easily produce 2 hours of trouble-shooting.
I will gladly help you programming. :D

I have reviewed your txtPesquisa with this result:
Code: Select all
Private Sub txtPesquisa_Change()
   
    If txtPesquisa.Text = "" Then
        RelatórioForm
    Else
        X = 1
        With ListView1.ListItems
            y = .Count
            While (X <= y) And (X < .Count)   ' without this 2nd condition X might become < 1
                If InStr(1, .Item(X), txtPesquisa.Text, vbTextCompare) = 0 Then
                    .Remove X      ' I don't think you need the brackets around the X here
                    X = X - 1
                    y = y - 1
                End If
                X = X + 1
            Wend
        End With
    End If
End Sub

I also had a look at your sub RelatórioForm. I'm not sure you wanted me to do that, but I did find a lot of irregularities which I have commented. Please read my comments.
Code: Select all
Sub RelatórioForm()
   
    ' dim lastRow as long   ' double declaration: see top of module
    Dim lastResultRow As Long
    Dim X As Long
    Dim C As Long   ' C = column
    Dim Lvx As Integer  ' = ListViewItem index
   
    ' this is part of the code
    ' it must FOLLOW the Dim statements
    ' on error resume next ' don't use this unless you want to handle the error
    ' you have no error handling device here.
    ' therefore you should let the error show
    Application.EnableEvents = False
    ' Verifica qual a ultima célula preenchida
    With Plan1
        ' count the rows in Plan1 !!!
        lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    End With
       
    lastResultRow = 2 'linha resultado
    Me.ListView1.ListItems.Clear

    ' Ciclo em todas as linhas
    For X = 2 To lastRow '1 Linha dados pequisa
        ' verifica se o valor é igual ao da pesquisa
        ' if any of these CDate conversions should fail your code will crash.
        ' Before applying this function you should examine them
        ' for their availability and capability to be converted.
        If CDate(Plan1.Cells(X, 5).Value) >= CDate(txtDataInicial.Value) _
           And CDate(Plan1.Cells(X, 5)) <= CDate(txtDataFinal) Then
            ' Copia os valores
            With ListView1.ListItems
                .Add 1, , Plan1.Cells(X, 1).Value
                Lvx = Lvx + 1
                For C = 2 To 12
                     Debug.Print X, C, Plan1.Cells(X, C).Value
                    .Item(Lvx).ListSubItems.Add C - 1, , Plan1.Cells(X, C).Value
                Next C
            End With
            lastResultRow = lastResultRow + 1
        End If
    Next
       
    Application.EnableEvents = True
End Sub

As you see, I have replaced the large block of repetitions with a simple loop. If you wish to exclude an item you should use an If Then function. I have added a Debug.Print statement in the loop in order to show you how to test. You will see the list being printed up to the point where an error occurs.

I didn't test either your code or mine. If you want me to test you should tell me what to enter in the various controls. Error 6 occurs when you call a a member of an array that doesn't exist. You should identify the counter that doesn't count as expected and eliminate the source of the error. Counting errors don't require experts. Instead, they need patience and thorough knowledge of your project. I take it that you have the knowledge. :D

Finally, I recommend you to use 'Option Explicit' at the top of each of your modules. In fact, you can set this in the VBE properties somewhere. Doing you will save you time chasing declaration and naming errors.
  • 0

Have a great day! :D

Sisyphus
I do this for "honour and country" - much less of the latter, actually.
If I helped you, award points, plenty of them.
If I bored you, deduct points for being too long-winded. (I know, :lol)
Sisyphus
Former Moderator
 
Posts: 4454
Joined: Dec 7, 2011
Location: Shanghai
Reputation: 203
Excel Version: 2010

Re: ListView does not filter

Postby marreco » Tue Jan 24, 2012 6:51 pm

Thanks, I'll follow your tips.

Thank you, you have helped me a lot.
  • 0

marreco
Regular
 
Posts: 51
Joined: Jan 13, 2012
Reputation: 0


Return to Macros and VBA Questions

Who is online

Users browsing this forum: No registered users and 139 guests