ListBox Change Selection (VBA)

gravanoc

Active Member
Joined
Oct 20, 2015
Messages
346
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
This seems like it is frustratingly simple, but I've had no luck with getting my ListBox to deselect or even change its current selection.
My ListBox is populated with all the tabs in the workbook (there are 36 tabs with very long names), so when they click the tab name in the ListBox it immediately transports them to that tab. That works fine. The problem is that if they return to the page with the ListBox the same tab name remains selected and cannot be selected again unless something else is selected first. This adds an unnecessary extra step into the process.

Two possible workaround solutions would be to either set the ListBox to have no selection, meaning nothing is highlighted, or to change the index to zero, which is the tab the ListBox is on.
Here is my current code, although I've tried several variations of it. Everything works EXCEPT for changing the selection.

Code:
Option Explicit

Private Sub ListBox1_Click()
    
    Dim wb As Workbook
    Dim MATTOC() As Worksheet
    Dim wsDest As Worksheet
    Dim wsCount As Integer
    Dim wsName As String
    
    Dim i As Integer


    Set wb = ThisWorkbook


    wsCount = Worksheets.Count
    ReDim MATTOC(1 To wsCount)
    For i = 1 To wsCount


        Set MATTOC(i) = Worksheets(i)


    Next


    wsName = MATTOC(4).Range("ToCChange").Value
    Set wsDest = Worksheets(wsName)
    ListBox1.ListIndex = 0
    wsDest.Activate
    wsDest.Select

End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Have you tried: ListBox1.ListIndex = -1
 
Upvote 0
Have you tried writing code to the Workbook_SheetActivate and Workbook_SheetDeActivate events in the ThisWorkbook code module. Changing the List box to reflect the user's manual changing of worksheets?

Sych things so that the ActiveSheet is always selected in the Listbox, no matter how it gets there?
 
Upvote 0
Don't put it in Sub ListBox1_Click
Try placing it in:

Code:
Private Sub ListBox1_LostFocus()
ListBox1.ListIndex = -1
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,779
Members
449,049
Latest member
greyangel23

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