ListView Filter

Rex2024

New Member
Joined
Nov 17, 2022
Messages
32
Office Version
  1. 365
Platform
  1. Windows
I am attempting to filter my listview upon populating based on an employee number. I can see the value is being retained, it's an employee number, but the system doesn't return anything.

VBA Code:
Sub UserForm_Activate()

Dim p As Long
Dim CLN As Integer
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("EventLog")
Dim x As Integer
Dim RNG As Variant
Dim C As Integer

EmpNum = EventLog.EmpNum.Value

With Me.EventHistoryLV

    .ListItems.Clear
    .ColumnHeaders.Clear
    .FullRowSelect = True

    .ColumnHeaders.Add , , "Event ID", 20
    .ColumnHeaders.Add , , "EmpNum", 50
    .ColumnHeaders.Add , , "LastName", 80
    .ColumnHeaders.Add , , "FirstName", 80
    .ColumnHeaders.Add , , "Details", 250
    .ColumnHeaders.Add , , "Update Time", 120

    For p = 1 To ws.Range("A1000000").End(xlUp).Row
        If ws.Range("B" & p).Value = EmpNum Then
        .ListItems.Add , , ws.Cells(p + 1, "A")
        CLN = CLN + 1
        .ListItems(CLN).ListSubItems.Add , , ws.Cells(p + 1, "B") '1
        .ListItems(CLN).ListSubItems.Add , , ws.Cells(p + 1, "C") '2
        .ListItems(CLN).ListSubItems.Add , , ws.Cells(p + 1, "D") '3
        .ListItems(CLN).ListSubItems.Add , , ws.Cells(p + 1, "E") '4
        .ListItems(CLN).ListSubItems.Add , , ws.Cells(p + 1, "F") '5
        End If
    Next p
    
End With

End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi,
Have you tried to use Private Sub UserForm_Initialize()

instead of Private Sub UserForm_Activate() ...?
 
Upvote 0
OK....
Could you test following modification for your loop instruction with :
For p = 1 To ws.Cells(Rows.Count, "A").End(xlUp).Row
 
Upvote 0
OK....
Could you test following modification for your loop instruction with :
For p = 1 To ws.Cells(Rows.Count, "A").End(xlUp).Row
No change. Here is the modified code:

VBA Code:
Sub UserForm_Initialize()

Dim p As Long
Dim CLN As Integer
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("EventLog")
Dim x As Integer
Dim RNG As Variant
Dim C As Integer

sEmp = EventLog.EmpNum.Value

With Me.EventHistoryLV

    .ListItems.Clear
    .ColumnHeaders.Clear
    .FullRowSelect = True

    .ColumnHeaders.Add , , "Event ID", 20
    .ColumnHeaders.Add , , "EmpNum", 50
    .ColumnHeaders.Add , , "LastName", 80
    .ColumnHeaders.Add , , "FirstName", 80
    .ColumnHeaders.Add , , "Details", 250
    .ColumnHeaders.Add , , "Update Time", 120


    For p = 1 To ws.Cells(Rows.Count, "A").End(xlUp).Row
        If ws.Range("B" & p).Value = sEmp Then
        .ListItems.Add , , ws.Cells(p + 1, "A")
        CLN = CLN + 1
        .ListItems(CLN).ListSubItems.Add , , ws.Cells(p + 1, "B") '1
        .ListItems(CLN).ListSubItems.Add , , ws.Cells(p + 1, "C") '2
        .ListItems(CLN).ListSubItems.Add , , ws.Cells(p + 1, "D") '3
        .ListItems(CLN).ListSubItems.Add , , ws.Cells(p + 1, "E") '4
        .ListItems(CLN).ListSubItems.Add , , ws.Cells(p + 1, "F") '5
        .ListItems(CLN).ListSubItems.Add , , ws.Cells(p + 1, "G") '6
        End If
    Next p
    
End With

End Sub
 
Upvote 0
Sorry ... but without the file with the UserForm ...cannot help any further :confused:
 
Upvote 0
Sorry ... but without the file with the UserForm ...cannot help any further :confused:
I figured out my issue. The p+1 was returning the line I wanted plus 1 line, which was incorrect. I had incorrectly applied the code. Removing the +1 returns the correct info.

Thanks for trying! I really appreciate it :)
 
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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