Selecting Sheets vidth Userform/Listbox trouble

tryagain

Board Regular
Joined
Mar 15, 2010
Messages
102
Hi experts im giveing up, ned help on this one :)


Got a Userform width a ListBox
ListBox1 is populated width Sheet names (6) Ark1,Ark2,Ark3,Ark4,Ark5 and Ark6
In the ListBox1 i check out which Sheet to select
Trouble is that the code below not only select my shoice in ListBox1 but also
the sheet that was active when i started up Userform
eg.:
Sheet Ark1 is active.
Now i run code and select Ark2 and Ark4 in the Listbox
But when i finish Sheet "Ark1 is still selected together width Ark2 and Ark4
How do i prevent sheet "Ark1" been selcted too ????
eg.:

If i select sheet Ark3 and run code and select Ark1 and Ark5 in ListBox1
then its ok only Ark1 and Ark5 is selected !!!

Private Sub UserForm_Click()
Dim t As Integer
Dim flag As Boolean
flag = True
For t = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(t) = True Then Sheets(Me.ListBox1.List(t)).Select flag
flag = False
Next
End Sub


Thanks in advance
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try this.
Code:
Dim strSheets As String
Dim t As Long
    
    For t = 0 To Me.ListBox1.ListCount - 1
        If Me.ListBox1.Selected(t) = True Then
            If strSheets = "" Then
                strSheets = Me.ListBox1.List(t)
            Else
                strSheets = strSheets & "," & Me.ListBox1.List(t)
            End If
        End If
    Next t
    
    Sheets(Split(strSheets, ",")).Select
 
Upvote 0
Hi Norie
Perfect solution thanks a lot ill use it for sure

But still wondering why my code not working, well newer mind i got one now lol

thanks again

Poul
 
Upvote 0
Poul

This isn't quite in the right position.
Code:
flag = False
You only want to set flag to False once the first sheet has selected, so try this.
Code:
Dim strSheets As String
Dim t As Long
Dim flag As Boolean

    flag = True
    
    For t = 0 To Me.ListBox1.ListCount - 1
        If Me.ListBox1.Selected(t) = True Then
            Sheets(Me.ListBox1.List(t)).Select flag
            flag = False
        End If
    Next t
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,862
Members
449,052
Latest member
Fuddy_Duddy

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