View Single Post
  #8   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?

Oops...bad news: i found an additional problem. The good news is that
i'll post the solution.

THE PROBLEM:
The code above works fine to update the list of worksheets when the
user adds or deletes worksheets or changes the order of existing
worksheets. However, the code does not function as expected when the
user changes the name of existing worksheets. For example, if the user
was to right click the worksheet tab and choose rename then rename the
sheet...or if the user just double-clicks the worksheet tab, renames
the sheet, and presses enter. In those examples, the listbox would not
be updated to reflect the sheet name change. In fact, no further
updates will occur. For whatever reason it seems like changing the
sheet name in those ways kills the OnTime calls.

THE SOLUTION:
***(Add this function to a cell on the worksheet)***
=MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,LEN(CELL("filename",A 1))-FIND("]",CELL("filename",A1)))

***(Add this Function to Module1)***
Public Function PagesHaveChanged() As Boolean
Dim tempChange As Boolean
Dim LB_List As Variant
Dim arrayIndex As Integer

''set Change = True (default)
tempChange = 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)

' PrintArrayToMsgBox (LB_List)

''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
tempChange = False
Else ''if any value is not equal, set change to True & Exit
For
tempChange = True
Exit For
End If
Next arrayIndex
End If

PagesHaveChanged = tempChange
End Function

***(Add this Sub to each worksheet code page)***
Private Sub Worksheet_Calculate()
If Module1.PagesHaveChanged Then
Application.OnTime Now + TimeValue("00:00:02"),
"ThisWorkbook.DoStuff"
End If
End Sub

***(Change Sub DoStuff() in ThisWorkbook to the following)***
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 (Module1.PagesHaveChanged)
change = Module1.PagesHaveChanged

''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

And that oughta do it. How it works: each time the sheet name is
changed, the sheet now has a cell that updates to reflect the sheet
name. The auto-update of that cell fires the Worksheet_Calculate
event. So, Worksheet_Calculate calls DoStuff() to continue the updates
of the listbox.

If anyone has questions about this code, please let me know. I can
send an example file with the functioning code.

Troy