View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
tr00per03 tr00per03 is offline
external usenet poster
 
Posts: 4
Default how to update Listbox of sheetnames if user changes sheetnames?

SOLUTION CODE:
ThisWorkbook:
Private Sub Workbook_Open()
''show UserForm1 when workbook opens
UserForm1.Show
End Sub

Public Sub DoStuff()
Dim RCGB As Boolean ''used as local RUN_CODE_GLOBAL_BOOLEAN
value
Dim LB_List As Variant ''used as array of items in
UserForm1.ListBox1
Dim Change As Boolean ''used to express whether worksheet
pages have changed
Dim arrayIndex As Integer ''used to increment through LB_List and
sheet names
Dim listIndex As Integer ''used to store users current
ListBox1.ListIndex

''get value for RCGB (ThisWorksheet.RUN_CODE_GLOBAL_BOOLEAN)
RCGB = Module1.getRCGB

''set Change = True (default)
Change = True
''check to see if worksheet pages (count,order,etc) have changed
since last update
'' only if the count of sheets = count of items in LB_List b/c
otherwise,
'' a change has definitely occurred
If UserForm1.ListBox1.ListCount = Sheets.Count Then
''fill LB_List with values in UserForm1.ListBox
LB_List = Module1.PopulateArrayWithListBox(UserForm1.ListBox 1)
''increment through the array items and compare each value with
sheet name
For arrayIndex = 0 To UserForm1.ListBox1.ListCount - 1
''if values are equal, set change to false and check
remaining values
If LB_List(arrayIndex) = Sheets(arrayIndex + 1).name Then
Change = False
Else ''if any value is not equal, set change to True & Exit
For
Change = True
Exit For
End If
Next arrayIndex
End If


''To reduce number of updates,
'' only update ListBox1 when RCGB = True
If RCGB = True Then
'' also, only update ListBox1 when Change = True
If Change = True Then
''get user's current ListBox1.listIndex - if none select,
then select 0
If UserForm1.ListBox1.listIndex < 0 Then
listIndex = 0
Else
listIndex = UserForm1.ListBox1.listIndex
End If

'clear ListBox1
UserForm1.ListBox1.Clear

'add sheet names to ListBox1
For arrayIndex = 0 To Sheets.Count - 1
UserForm1.ListBox1.AddItem (Sheets(arrayIndex +
1).name)
Next arrayIndex

''re-select user's selection
'' this code could be better, suggest: use value instead
of index
UserForm1.ListBox1.listIndex = listIndex
UserForm1.ListBox1.Selected(listIndex) = True
End If

''call DoStuff again to detect future changes user may make
Application.OnTime Now + TimeValue("00:00:02"),
"ThisWorkbook.DoStuff"
End If
End Sub

UserForm1 CODE:
Private Sub MultiPage1_Change()
Dim page As Integer

page = MultiPage1.value

''start DoStuff if page = 1
If page = 1 Then
Module1.setRCGB (True)
Application.OnTime Now, "ThisWorkbook.DoStuff"
''stop DoStuff if page < 1
Else
Module1.setRCGB (False)
Application.OnTime Now, "ThisWorkbook.DoStuff", False
End If
End Sub

Private Sub UserForm_Initialize()
''start MultiPage1 on page 0 (first page tab)
MultiPage1.value = 0
''set RUN_CODE_GLOBAL_BOOLEAN = True (ThisWorkbook.)
Module1.setRCGB (True)
End Sub

Module1 CODE:
Dim RUN_CODE_GLOBAL_BOOLEAN As Boolean

Public Sub setRCGB(tf As Boolean)
RUN_CODE_GLOBAL_BOOLEAN = tf
End Sub

Public Function getRCGB() As Boolean
getRCGB = RUN_CODE_GLOBAL_BOOLEAN
End Function

Public Function PopulateArrayWithListBox(inListBox As Variant)
Dim arrayIndex As Integer
Dim tempArray As Variant

ReDim tempArray(inListBox.ListCount)

For arrayIndex = 0 To UBound(tempArray) - 1
tempArray(arrayIndex) = inListBox.List(arrayIndex)
Next arrayIndex

PopulateArrayWithListBox = tempArray
End Function

Description:
I created a userform (UserForm1) that contains a MultiPage (MultiPage1)
with two pages (Page1 & Page2). Page1 contains nothing. Page2 has a
ListBox control (ListBox1). Some of the listbox properties are very
important: MultiSelect = 0 - fmMultiSelectSingle. With this code, the
UserForm will display upon opening the workbook. When the user selects
Page2, the listbox is updated with the list of current workbook sheet
names. As long as Page2 is selected, listbox will continue to update
(approximately every 2 seconds...code: Application.OnTime.... To
minimize updating, updates will not occur when sheets in the workbook
have not changed since last update...code: DoStuff sets change = True
if the sheet names, order, etc have changed.

There were many challenges in getting this project to work correctly,
but as you can see, the code is not necessarily pretty/concise/etc;
However, it should suffice as a workaround to the problem of displaying
and updating a listbox of workbook page names.

If you have any questions, please contact me. Also, if you would like
to have a copy of this workbook example emailed to you, I can do that
as well.

One more solution - so many more problems. Happy programming.

Troy