E
Eric
I have a dynamic array designed to hold a user defined type, both of which
are declared as follows:
Private m_filterRestore() As udt_filterRestoreData
Private Type udt_filterRestoreData
m_wksName As String
m_colName As String
m_index As Integer
m_filter As Filter
End Type
When I run a test in the debugger, stepping through it, the array is
correctly dimensioned (for this test) with elements 0 through 4. When I let
it run without stepping through it, however, it is dimensioned with elements
0 through 10, with elements 5 through 10 holding empty udts. The code is
below, and I sure don't get it.
This is a long one, so I really do appreciate the time.
Thanks, Eric
--------------------------------------------------------------------------------------------
' get filter settings for all worksheets in the workbook
Public Function SaveFilterSettingsForWorkbook(aWkbk As Workbook) As Integer
Dim iResult As Integer
Dim wkbkArray As Variant
If WorkbookWorksheetsToArray(aWkbk, wkbkArray, True) > 0 Then
Dim i As Integer
For i = LBound(wkbkArray) To UBound(wkbkArray)
iResult = iResult +
SaveFilterSettingsForWorksheet(aWkbk.Worksheets(wkbkArray(i)))
Next i
End If
SaveFilterSettingsForWorkbook = iResult
'test
For i = 0 To UBound(m_filterRestore)
Dim s As String
s = m_filterRestore(i).m_wksName & DELIM &
m_filterRestore(i).m_index & DELIM & _
m_filterRestore(i).m_filter.Criteria1 & DELIM
If m_filterRestore(i).m_filter.Operator Then
s = s & m_filterRestore(i).m_filter.Operator & DELIM &
m_filterRestore(i).m_filter.Criteria2
Else
s = s & "no criteria2"
End If
Debug.Print s
Next i
End Function
----------------------------------------------------------------------------------
'get filter settings for a worksheet
Public Function SaveFilterSettingsForWorksheet(aWks As Worksheet) As Integer
Dim fltrs As Filters
Set fltrs = aWks.AutoFilter.Filters
If Not IsNothing(fltrs) Then
Dim wksName As String
wksName = aWks.Name
Dim restoreIndex As Integer
On Error Resume Next ' in case the array is empty
Dim bFirstAdd As Boolean
bFirstAdd = IsNothing(m_filterRestore(0).m_wksName)
If Err.Number = 9 Then
ReDim m_filterRestore(0)
bFirstAdd = True
End If
restoreIndex = IIf(bFirstAdd, UBound(m_filterRestore),
UBound(m_filterRestore) + 1)
ReDim Preserve m_filterRestore(restoreIndex + (fltrs.Count))
Dim f As Filter, i As Integer, nbrAdded As Integer
For Each f In fltrs
If f.On Then
Dim restoreData As udt_filterRestoreData
restoreData.m_wksName = wksName
restoreData.m_index = i + 1 ' our restoreArray is 0 based
but the fltr collection is not
Set restoreData.m_filter = f
nbrAdded = nbrAdded + 1
m_filterRestore(restoreIndex + (nbrAdded - 1)) = restoreData
End If
i = i + 1
Next f
If nbrAdded > 0 Then
ReDim Preserve m_filterRestore(restoreIndex + (nbrAdded - 1))
Else
ReDim Preserve m_filterRestore(restoreIndex)
End If
SaveFilterSettingsForWorksheet = nbrAdded
End If
'test
' Dim bNothingNew As Boolean
' bNothingNew = IsNothing(m_filterRestore(restoreIndex).m_wksName)
' If bNothingNew Then Exit Function
' For i = restoreIndex To UBound(m_filterRestore)
' Dim s As String
' ' if the array is empty let's split
' ' it's not empty
' s = m_filterRestore(i).m_wksName & DELIM &
m_filterRestore(i).m_index & DELIM & _
' m_filterRestore(i).m_filter.Criteria1 & DELIM
' If m_filterRestore(i).m_filter.Operator Then
' s = s & m_filterRestore(i).m_filter.Operator & DELIM &
m_filterRestore(i).m_filter.Criteria2
' Else
' s = s & "no criteria2"
' End If
' Debug.Print s
' Next i
End Function
are declared as follows:
Private m_filterRestore() As udt_filterRestoreData
Private Type udt_filterRestoreData
m_wksName As String
m_colName As String
m_index As Integer
m_filter As Filter
End Type
When I run a test in the debugger, stepping through it, the array is
correctly dimensioned (for this test) with elements 0 through 4. When I let
it run without stepping through it, however, it is dimensioned with elements
0 through 10, with elements 5 through 10 holding empty udts. The code is
below, and I sure don't get it.
This is a long one, so I really do appreciate the time.
Thanks, Eric
--------------------------------------------------------------------------------------------
' get filter settings for all worksheets in the workbook
Public Function SaveFilterSettingsForWorkbook(aWkbk As Workbook) As Integer
Dim iResult As Integer
Dim wkbkArray As Variant
If WorkbookWorksheetsToArray(aWkbk, wkbkArray, True) > 0 Then
Dim i As Integer
For i = LBound(wkbkArray) To UBound(wkbkArray)
iResult = iResult +
SaveFilterSettingsForWorksheet(aWkbk.Worksheets(wkbkArray(i)))
Next i
End If
SaveFilterSettingsForWorkbook = iResult
'test
For i = 0 To UBound(m_filterRestore)
Dim s As String
s = m_filterRestore(i).m_wksName & DELIM &
m_filterRestore(i).m_index & DELIM & _
m_filterRestore(i).m_filter.Criteria1 & DELIM
If m_filterRestore(i).m_filter.Operator Then
s = s & m_filterRestore(i).m_filter.Operator & DELIM &
m_filterRestore(i).m_filter.Criteria2
Else
s = s & "no criteria2"
End If
Debug.Print s
Next i
End Function
----------------------------------------------------------------------------------
'get filter settings for a worksheet
Public Function SaveFilterSettingsForWorksheet(aWks As Worksheet) As Integer
Dim fltrs As Filters
Set fltrs = aWks.AutoFilter.Filters
If Not IsNothing(fltrs) Then
Dim wksName As String
wksName = aWks.Name
Dim restoreIndex As Integer
On Error Resume Next ' in case the array is empty
Dim bFirstAdd As Boolean
bFirstAdd = IsNothing(m_filterRestore(0).m_wksName)
If Err.Number = 9 Then
ReDim m_filterRestore(0)
bFirstAdd = True
End If
restoreIndex = IIf(bFirstAdd, UBound(m_filterRestore),
UBound(m_filterRestore) + 1)
ReDim Preserve m_filterRestore(restoreIndex + (fltrs.Count))
Dim f As Filter, i As Integer, nbrAdded As Integer
For Each f In fltrs
If f.On Then
Dim restoreData As udt_filterRestoreData
restoreData.m_wksName = wksName
restoreData.m_index = i + 1 ' our restoreArray is 0 based
but the fltr collection is not
Set restoreData.m_filter = f
nbrAdded = nbrAdded + 1
m_filterRestore(restoreIndex + (nbrAdded - 1)) = restoreData
End If
i = i + 1
Next f
If nbrAdded > 0 Then
ReDim Preserve m_filterRestore(restoreIndex + (nbrAdded - 1))
Else
ReDim Preserve m_filterRestore(restoreIndex)
End If
SaveFilterSettingsForWorksheet = nbrAdded
End If
'test
' Dim bNothingNew As Boolean
' bNothingNew = IsNothing(m_filterRestore(restoreIndex).m_wksName)
' If bNothingNew Then Exit Function
' For i = restoreIndex To UBound(m_filterRestore)
' Dim s As String
' ' if the array is empty let's split
' ' it's not empty
' s = m_filterRestore(i).m_wksName & DELIM &
m_filterRestore(i).m_index & DELIM & _
' m_filterRestore(i).m_filter.Criteria1 & DELIM
' If m_filterRestore(i).m_filter.Operator Then
' s = s & m_filterRestore(i).m_filter.Operator & DELIM &
m_filterRestore(i).m_filter.Criteria2
' Else
' s = s & "no criteria2"
' End If
' Debug.Print s
' Next i
End Function