Hi,
20 minutes of programming can easily produce 2 hours of trouble-shooting.
I will gladly help you programming.
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.
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.
Have a great day!
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)