bearcub
Well-known Member
- Joined
- May 18, 2005
- Messages
- 734
- Office Version
- 365
- 2013
- 2010
- 2007
- Platform
- Windows
I have a list box that I have on a userform that lists all the workbook sheets and then goes to the sheet that is selected. I'm trying to alphabetically sort the sheets in the sheet box. I found a code snippet on the internet but I'm not sure where to call or put the macro. Here are the code snippets that I have. How do I incorporate the sort code into the main code
I tried to call both the function and the sub into the list box code but nothing seems to work. I'm scratching my head trying to figure out how to incorporate the array code into the main code.
Thank you for your help,
Michael
Rich (BB 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
Call RunTheSortMacro
Next i
Sheets(sht).Activate
End
End Sub
This is the Sort Code that I found this morning that I don't know how to incorporate into the listbox code
Sub RunTheSortMacro()
Dim i As Long
Dim myArray As Variant
'Set the array
myArray = Array("p", "A", "G", 3, "l", "6", 10, "K", 7)
'myArray variable set to the result of SortArrayAtoZ function
myArray = SortArrayAtoZ(myArray)
'Output the Array through a message box
For i = LBound(myArray) To UBound(myArray)
MsgBox myArray(i)
Next i
End Sub
Function SortArrayAtoZ(myArray As Variant)
Dim i As Long
Dim j As Long
Dim Temp
'Sort the Array A-Z
For i = LBound(myArray) To UBound(myArray) - 1
For j = i + 1 To UBound(myArray)
If UCase(myArray(i)) > UCase(myArray(j)) Then
Temp = myArray(j)
myArray(j) = myArray(i)
myArray(i) = Temp
End If
Next j
Next i
SortArrayAtoZ = myArray
End Function
I tried to call both the function and the sub into the list box code but nothing seems to work. I'm scratching my head trying to figure out how to incorporate the array code into the main code.
Thank you for your help,
Michael