Hello all
I have been working on a combobox for a toolbar that seems to have an erratic performance. It works quite happily for a while and then throws up an out of range error.
It uses a Public array to populate the choices with sheet names and then use those values to select a sheet based on the choice from the combo box.
Its slightly clumsy so maybe could do with a tidy up but why does it only fail occasionally, usually when I think its working fine. Is there something I cant see?
Anyway here it is any suggestions or improvements would be nice, especially the combobox reset which I cant seem to get working.
Public ws As Worksheet
Public SheetArray(1 To 10) As String
Public i As Integer ' used as array index value
Sub ToAddBars() 'creates browse toolbar and sets properties etc
For Each Toolbar In Toolbars
Select Case Toolbar.Name
Case "test"
Toolbar.Delete
End Select
Next
Set mybar = CommandBars.Add(Name:="test", Position:=msoBarTop, Temporary:=True)
mybar.Visible = True
Set newcombo = mybar.Controls.Add(Type:=msoControlComboBox)
With newcombo
.AddItem "Select worksheet to view"
For Each ws In Worksheets
i = i + 1
.AddItem ws.Name
Let SheetArray(i) = ws.Name
Next ws
.DropDownWidth = 120
.ListIndex = 1
.Caption = "Class Group Filter"
.Width = 140
.OnAction = "processselection"
End With
End Sub
Sub processselection() 'processes options chosen from drop down box for form.
Dim choice As Long
choice = CommandBars("test").Controls(1).ListIndex
Select Case choice
Case 1
choice = 1
Case 2
choice = 2
choice = choice - 1'had to do this to ensure correct array value not sure why bit clumsy.
Sheets(SheetArray(choice)).Select
Case 3
choice = 3
choice = choice - 1
Sheets(SheetArray(choice)).Select
Case 4
choice = 4
choice = choice - 1
Sheets(SheetArray(choice)).Select
Case 5
choice = 5
choice = choice - 1
Sheets(SheetArray(choice)).Select
Case 6
choice = 6
choice = choice - 1
Sheets(SheetArray(choice)).Select
Case 7
choice = 7
choice = choice - 1
Sheets(SheetArray(choice)).Select
Case 8
userchoice = 8
choice = choice - 1
Sheets(SheetArray(choice)).Select
Case 9
userchoice = 9
choice = choice - 1
Sheets(SheetArray(choice)).Select
Case 10
userchoice = 10
choice = choice - 1
Sheets(SheetArray(choice)).Select
End Select
End Sub
Many thanks in advance
Neil
I have been working on a combobox for a toolbar that seems to have an erratic performance. It works quite happily for a while and then throws up an out of range error.
It uses a Public array to populate the choices with sheet names and then use those values to select a sheet based on the choice from the combo box.
Its slightly clumsy so maybe could do with a tidy up but why does it only fail occasionally, usually when I think its working fine. Is there something I cant see?
Anyway here it is any suggestions or improvements would be nice, especially the combobox reset which I cant seem to get working.
Public ws As Worksheet
Public SheetArray(1 To 10) As String
Public i As Integer ' used as array index value
Sub ToAddBars() 'creates browse toolbar and sets properties etc
For Each Toolbar In Toolbars
Select Case Toolbar.Name
Case "test"
Toolbar.Delete
End Select
Next
Set mybar = CommandBars.Add(Name:="test", Position:=msoBarTop, Temporary:=True)
mybar.Visible = True
Set newcombo = mybar.Controls.Add(Type:=msoControlComboBox)
With newcombo
.AddItem "Select worksheet to view"
For Each ws In Worksheets
i = i + 1
.AddItem ws.Name
Let SheetArray(i) = ws.Name
Next ws
.DropDownWidth = 120
.ListIndex = 1
.Caption = "Class Group Filter"
.Width = 140
.OnAction = "processselection"
End With
End Sub
Sub processselection() 'processes options chosen from drop down box for form.
Dim choice As Long
choice = CommandBars("test").Controls(1).ListIndex
Select Case choice
Case 1
choice = 1
Case 2
choice = 2
choice = choice - 1'had to do this to ensure correct array value not sure why bit clumsy.
Sheets(SheetArray(choice)).Select
Case 3
choice = 3
choice = choice - 1
Sheets(SheetArray(choice)).Select
Case 4
choice = 4
choice = choice - 1
Sheets(SheetArray(choice)).Select
Case 5
choice = 5
choice = choice - 1
Sheets(SheetArray(choice)).Select
Case 6
choice = 6
choice = choice - 1
Sheets(SheetArray(choice)).Select
Case 7
choice = 7
choice = choice - 1
Sheets(SheetArray(choice)).Select
Case 8
userchoice = 8
choice = choice - 1
Sheets(SheetArray(choice)).Select
Case 9
userchoice = 9
choice = choice - 1
Sheets(SheetArray(choice)).Select
Case 10
userchoice = 10
choice = choice - 1
Sheets(SheetArray(choice)).Select
End Select
End Sub
Many thanks in advance
Neil