Restricting a vba listbox

KDS14589

Board Regular
Joined
Jan 10, 2019
Messages
180
Office Version
  1. 2016
Platform
  1. Windows
It's been a long day……I have a listbox (code below) on a userform which produces a list of all worksheets in the workbook. But I want that list to not include the active worksheet the userform is called from. It is always called from codename ShGE04 and named General. Switchboard
A image of it is below

Given time I could work this out, but right now time is something I don't have.



VBA Code:
Private Sub CommandButton1_Click()

Dim i As Integer, sht As String

For i = 0 To ListBox1.ListCount - 1

If ListBox1.Selected(i) = True Then

sht = ListBox1.List(i)

End If

Next i

Sheets(sht).Visible = True

ShGE04.Visible = False

Sheets(sht).Activate

End

End Sub
 

Attachments

  • Mr Excel 9.7.21  .png
    Mr Excel 9.7.21 .png
    38.8 KB · Views: 7

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I want that list to not include the active worksheet the userform is called from.
try this in UserForm_Initialize
VBA Code:
Dim sh As Worksheet, shtnames As String

For Each sh In ThisWorkbook.Worksheets
    If sh.Name <> ActiveSheet.Name Then
        shtnames = shtnames & "|" & sh.Name
    End If
Next sh

ListBox1.List = Split(Mid(shtnames, 2), "|")
 
Upvote 0
Solution
try this in UserForm_Initialize
VBA Code:
Dim sh As Worksheet, shtnames As String

For Each sh In ThisWorkbook.Worksheets
    If sh.Name <> ActiveSheet.Name Then
        shtnames = shtnames & "|" & sh.Name
    End If
Next sh

ListBox1.List = Split(Mid(shtnames, 2), "|")
It works, my problem was I was trying to modify the wrong code, the code for the Go To Worksheet button, as I said it was a long frustrating day.

THANKS
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,421
Members
448,961
Latest member
nzskater

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