Hi,
I am building a tool that contains a list of worksheets that the user can choose to either "Exclude" or resequence in their preferred order.
I am hoping to learn how this can be achieved using arrays and have so far developed the following:
Are there better ways of achieving what i have done so far?
Can anyone help with sorting the array or testing if a value exists in it?
Thank you,
Mark
I am building a tool that contains a list of worksheets that the user can choose to either "Exclude" or resequence in their preferred order.
I am hoping to learn how this can be achieved using arrays and have so far developed the following:
Code:
Sub test_001()
Dim IndexCell As Range
Dim intCounter As Integer
'Data is stored in columns C and D, starting on row 9
'column D is a pre-populated unique list of sheet names
'column C is populated by a user selecting either "Exclude" or a number (i.e. the user is selecting which sheets they want to exclude and what sequence the sheets they need should be in)
intCounter = 0
'Count how many 'rows' i need in the array -------START
Set IndexCell = Range("C9")
Do While Not IsEmpty(IndexCell)
If IndexCell = "Exclude" Then
'do nothing (I don't need to count sheets I will exclude)
Else
intCounter = intCounter + 1
End If
Set IndexCell = IndexCell.Offset(1, 0)
Loop
'Count how many 'rows' i need in the array -------END
'Build the array --------------------------------------START
' Declare a two dimensional array
ReDim arrSheetsToKeep(0 To (intCounter - 1), 0 To 1) As String
intCounter = 0
Set IndexCell = Range("C9")
Do While Not IsEmpty(IndexCell)
If IndexCell = "Exclude" Then
'do nothing (I don't need to count sheets I will exclude)
Else
arrSheetsToKeep(intCounter, 0) = CStr(IndexCell.Value)
arrSheetsToKeep(intCounter, 1) = CStr(IndexCell.Offset(0, 1).Value)
intCounter = intCounter + 1
End If
Set IndexCell = IndexCell.Offset(1, 0)
Loop
'Build the array --------------------------------------END
'Write the array back to cells (so that i can see if it worked)
Range(Cells(9, 8), Cells(9 + intCounter - 1, 9)).Value = arrSheetsToKeep
' Next, I need to sort the array so that I know what needs to be sheet 1, sheet 2 etc.
' Then, I need to know how to loop through all the worksheets in the activebook and either hide them if they are NOT in the array, or read the required sheet position from the array if they ARE
End Sub
Are there better ways of achieving what i have done so far?
Can anyone help with sorting the array or testing if a value exists in it?
Thank you,
Mark
Last edited by a moderator: