VBA Userform itterating through listbox not working properly... VBA must be broken!

bryner

New Member
Joined
Nov 15, 2011
Messages
11
Ok, this is giving me a headache.

I thought it wouldn't be too hard to make a small userform with some code that would simply hide/unhide rows based on the selection of those rows in a listbox.

Basically I have a table with labels in column C. i want to be able to bring up a simple userform that autopopulates the labels... and has checkboxes so i can toggle which of these rows is visible (multiple selections possible). Currently I've been able to populate the listbox... and, on initialize, populate each item in the list with checks if they are already visible, or no checks if they are hidden... the problem is, when making any change.... it makes the first change (making the first item in the list either (correctly) visible or hidden... but then for some reason, the whole rest of the list becomes unchecked!?

I've worked out it has come down to the following line of code (i think):
Rows(r + t).Hidden = False
... because as soon as I comment it... then i can get a feed of the whole list that correctly identifies whether the checkbox is selected or not for each item.

Here is pretty much all the code from the "setvisiblestats userform"... it's pretty simple really... i left the debug lines in there so you can see that I'm not crazy.


Code:
Private Sub UserForm_Initialize()
    Dim r As Integer
    Dim t As Integer
    
    t = Application.WorksheetFunction.Match(lbStats.List(0), Range("C:C"), 0)
    
    For r = 0 To lbStats.ListCount - 1
        If Rows(r + t).Hidden = True Then
            lbStats.Selected(r) = False
        Else
            lbStats.Selected(r) = True
        End If
     Next r
End Sub




Private Sub cmdSUBMIT_Click()
    Application.ScreenUpdating = False
    ThisWorkbook.Worksheets("main").Activate
    
    Dim r As Integer
    Dim t As Integer


    'aligns the index with the first value of the list
    t = Application.WorksheetFunction.Match(lbStats.List(0), Range("C:C"), 0)


    For r = 0 To lbStats.ListCount - 1
[I]        Debug.Print "BEFORE IF row" & r & lbStats.Selected(r)[/I]


        If lbStats.Selected(r) = True Then
[COLOR=#0000ff][/COLOR][I]Debug.Print "UNHIDE before action - row" & r & lbStats.Selected(r) & " next is " & lbStats.Selected(r + 1)[/I][COLOR=#0000ff][I] 'this shows the checkbox status of the current and next item[/I][/COLOR]
            [B][COLOR=#ff0000]Rows(r + t).Hidden = False [/COLOR][/B]    [COLOR=#0000ff]' THIS IS THE LINE THAT IS ANNOYING MEEEEEE!!!!!!!![/COLOR]
[COLOR=#0000ff][/COLOR][I] Debug.Print "UNHIDE after action -  row" & r & lbStats.Selected(r) & " next is " & lbStats.Selected(r + 1)[/I][COLOR=#0000ff][I] 'as above, but AFTER the previous action[/I][/COLOR]
        Else
            Rows(r + t).Hidden = True
[I]            Debug.Print "HIDE row" & r; lbStats.Selected(r) & " next is " & lbStats.Selected(r + 1)[/I]
        End If


    Next r
    
    resetForm
End Sub

Please help! :confused:
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
*bump*

Alternatively... if anyone can throw me a line for code that would give me the same sort of functionality in another way... I'd take that too.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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