Restricting a vba listbox

KDS14589

Board Regular
Joined
Jan 10, 2019
Messages
138
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: 4

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

NoSparks

Well-known Member
Joined
Mar 15, 2013
Messages
1,173
Office Version
  1. 2010
Platform
  1. Windows
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), "|")
 
Solution

KDS14589

Board Regular
Joined
Jan 10, 2019
Messages
138
Office Version
  1. 2016
Platform
  1. Windows
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
 

Forum statistics

Threads
1,141,292
Messages
5,705,529
Members
421,399
Latest member
hjweiss00

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
Top