Restrict search for VBA listbox

KDS14589

Board Regular
Joined
Jan 10, 2019
Messages
180
Office Version
  1. 2016
Platform
  1. Windows
I'm using code I received lately and now trying to restrict it to only worksheets ending in LISTS for a new listbox on a new userform.

The original code is

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



Private Sub CommandButton2_Click()

Unload Go_To_WS

End Sub





Private Sub UserForm_Initialize()

''''NoSparks @ Mr Excel 9.6.21



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), "|")

End Sub



My new NONWORKING code is



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



Private Sub CommandButton2_Click()

Unload Edit_Lists_UF

End Sub



Private Sub UserForm_Initialize()



Dim sh As Worksheet, shtnames As String

Dim ws As Worksheet

For Each sh In ThisWorkbook.Worksheets

If ws.Name Like "*.lists" Then

shtnames = ws.Name

End If

Next sh



End Sub



I'm attempting to have a VBA listbox that shows only worksheets that end in .LISTS

I've checked other forums searches (along with yours) and the web about listboxs but they seem to involve results from other sources. So simple a task and I'm lost.

After many fail attempts, I'm asking for help again. THANKS
 

Attachments

  • Mr Excel 9.10.21   32pm  png.png
    Mr Excel 9.10.21 32pm png.png
    46.2 KB · Views: 11
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
You obviously have not been running the code that I have provided. There is no way possible that you can get your resulting names from my code.
 
Upvote 0
Let's try one more time.

On one of your sheets, press Alt-F11.
At the top of the window that pops up, click the insert tab, then select module.
On the right side of the window that pops up, paste the following code:

VBA Code:
Option Compare Text

Sub SheetNameTest()
    Dim sh As Worksheet, shtnames As String
'
    For Each sh In ThisWorkbook.Worksheets
        If Right(sh.Name, 5) = "LISTS" Then
            shtnames = shtnames & "|" & sh.Name
        End If
    Next sh
'
MsgBox shtnames
Debug.Print shtnames
End Sub

Then go back to one of your sheets and press Alt-F8.
In the box that pops up select 'SheetNameTest' and then select the 'Run' button.

Edit: Correction made


That code will display to a message box as well as the 'Immediate window' in the VBE.
 
Upvote 0
what is a sheetname that you have that hast LISTS in the name?

Something is not right because you said it was displaying all sheet names, now you say it is showing none.
with the full list shown is the original code, yours show none now, i just rerun code too make sure it was your result, right now should show one sheet 'cards.lists'
 
Upvote 0
with the full list shown is the original code, yours show none now, i just rerun code too make sure it was your result, right now should show one sheet 'cards.lists'
I adapted the code to adapt to your naming. latest code should show the one result that hast lists in the name.
 
Upvote 0
Let's try one more time.

On one of your sheets, press Alt-F11.
At the top of the window that pops up, click the insert tab, then select module.
On the right side of the window that pops up, paste the following code:

VBA Code:
Option Compare Text

Sub SheetNameTest()
    Dim sh As Worksheet, shtnames As String
'
    For Each sh In ThisWorkbook.Worksheets
        If Right(sh.Name, 5) = "LISTS" Then
            shtnames = shtnames & "|" & sh.Name
        End If
    Next sh
'
MsgBox shtnames
Debug.Print shtnames
End Sub

Then go back to one of your sheets and press Alt-F8.
In the box that pops up select 'SheetNameTest' and then select the 'Run' button.

Edit: Correction made


That code will display to a message box as well as the 'Immediate window' in the VBE.
 
Upvote 0

Forum statistics

Threads
1,214,535
Messages
6,120,090
Members
448,944
Latest member
sharmarick

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