populate ListBox depending on values in two columns

johnsonk

Board Regular
Joined
Feb 4, 2019
Messages
172
Hi,
I am trying to populate a listbox in a userform that ignores the value 0 in two colums C & D so it only populates the listbox with the rows that have a value greater than 0 in columns C & D. The image is an example of the data so what I would need from that is to just show the two rows. Hope this makes sense.

1602763891609.png


Regards

Also posted here Populate listbox in userform from worksheet ignoring 0 from two columns - OzGrid Free Excel/VBA Help Forum
 
Last edited by a moderator:
Hi, I managed to get the formula to work from the last column +1 but I'm still getting the autofilter error.
1603382954563.png
1603383023955.png
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
your range to filter is no longer .cells(1).currentregion
it now starts at A6 and goes to last row (lr) and the column with the formulas (lc + 1)
 
Upvote 0
I have made the changes but still getting the error, this is so confusing me right now.

VBA Code:
[/B]
Private Sub UserForm_Initialize()
Worksheets("ALL").Unprotect "?????"
With Application
.WindowState = xlMaximized
Zoom = Int(.Width / Me.Width * 85)
Width = .Width
Height = .Height
End With
    Dim lr As Long, lc As Long
Dim filtrng As Range

Application.ScreenUpdating = False
With ThisWorkbook.Sheets("ALL")
'remove any existing filters
If .FilterMode Then .ShowAllData
'determine last row
lr = .Range("A" & .Rows.Count).End(xlUp).Row
'determine last column
lc = .Cells(1, .Columns.Count).End(xlToLeft).Column
'put formula in next column
Range(.Cells(7, lc + 19), .Cells(lr, lc + 19)).FormulaR1C1 = "=RC[-2]+RC[-1]"
'filter on new column
Set filtrng = .Cells(6).CurrentRegion
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
With filtrng
.AutoFilter Field:=lc + 19, Criteria1:="<>0"
'check that count of filtered rows isn't zero
If .Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1 = 0 Then
'remove filter
.AutoFilter
'remove added column contents
Columns(lc + 19).ClearContents
Exit Sub
End If
End With
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End With

'add temp sheet to copy to
Sheets.Add
With ActiveSheet
.Name = "Scratchpad"
filtrng.Offset(1).Resize(lr - 1, lc).Copy
.Paste
'populate list box
Me.ListBox1.List = Sheets("Scratchpad").Cells(6).CurrentRegion.Value
'remove sheet
Application.DisplayAlerts = False
Sheets("Scratchpad").Delete
Application.DisplayAlerts = True
End With
'restore sheet1 to original
With ThisWorkbook.Sheets("ALL")
'remove filter
filtrng.AutoFilter
'clear the added column
.Columns(lc + 19).ClearContents
End With

Application.ScreenUpdating = True
Worksheets("ALL").Protect "?????"
End Sub
[B]
 
Upvote 0
VBA Code:
Private Sub UserForm_Initialize()
    Dim lr As Long, lc As Long
    Dim filtrng As Range
Application.ScreenUpdating = False
With ThisWorkbook.Sheets("ALL")
    If .FilterMode Then .ShowAllData
    lr = .Range("A" & .Rows.Count).End(xlUp).Row
    lc = .Cells(6, .Columns.Count).End(xlToLeft).Column
    Range(.Cells(7, lc + 1), .Cells(lr, lc + 1)).FormulaR1C1 = "=RC[-2]+RC[-1]"
    Set filtrng = .Range("A6", .Cells(lr, lc + 1))
    With filtrng
        .AutoFilter Field:=lc + 1, Criteria1:="<>0"
        If .Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1 = 0 Then
            .AutoFilter
            Columns(lc + 1).ClearContents
            Exit Sub
        End If
    End With
End With
Sheets.Add
With ActiveSheet
    .Name = "Scratchpad"
    filtrng.Offset(1).Resize(lr - 1, lc).Copy
    .Paste
    Me.ListBox1.List = Sheets("Scratchpad").Cells(1).CurrentRegion.Value
    Application.DisplayAlerts = False
    Sheets("Scratchpad").Delete
    Application.DisplayAlerts = True
End With
With ThisWorkbook.Sheets("ALL")
    filtrng.AutoFilter
    .Columns(lc + 1).ClearContents
End With
Application.ScreenUpdating = True
End Sub

sample file
 
Upvote 0
Thank you NoSparks but unfortunately am getting the error
1603458919542.png

from the line below

Me.ListBox1.List = Sheets("Scratchpad").Cells(1).CurrentRegion.Value

I have noticed the code now has the line - Set filtrng = .Range("A6", .Cells(lr, lc + 1))
where before it had - Set filtrng = .Cells(1).CurrentRegion
could this be the reason for the error.
 
Upvote 0
Are you getting that error in the file I linked to or in another file ?
 
Upvote 0
Solution
Sorry did not notice the link, but by using your link I have now got my form working perfect again. Thank you NoSpark for being patient and helping me getting it working again.
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,639
Members
449,093
Latest member
Ahmad123098

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