Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
n = 1
Do
on error resume next
if lbound(myarray, n) > 0 then n = n+ 1 else n = n + 1
if err.number = 9 then exit do
loop
msgbox "my array is " & n & "-dimensional"
Option Explicit
Public MasterCollection As Collection
Public MasterSheetIndex(400) As String
Sub test()
Dim sht As Worksheet
Dim i As Integer
Set MasterCollection = New Collection
For Each sht In Worksheets
GetData sht
Next sht
For i = 1 To MasterCollection.Count
ShowData i
Next i
Set MasterCollection = Nothing
End Sub
Private Sub GetData(ASheet As Worksheet)
' Get data from one sheet:
' save data in a collection
' add collection to MasterCollection
' store sheet name in MasterSheetIndex
Dim oCollection As New Collection
With ASheet
oCollection.Add (.Range("A1").Value)
oCollection.Add (.Range("A2").Value)
oCollection.Add (.Range("A3").Value)
MasterCollection.Add oCollection, .Name
MasterSheetIndex(MasterCollection.Count) = .Name
End With
End Sub
Private Sub ShowData(Index As Integer)
' Show data from one collection
Dim oCollection As Collection
Dim i As Integer
Set oCollection = MasterCollection(Index)
For i = 1 To oCollection.Count
MsgBox MasterSheetIndex(Index) & " " & oCollection(i)
Next i
End Sub
Option Explicit
Public MasterCollection As Collection
Sub test()
Dim sht As Worksheet
Dim i As Integer
Dim col As Collection
Set MasterCollection = New Collection
' Loop thru all sheets to find stuff to be processed
For Each sht In Worksheets
GetData sht
Next sht
' Loop thru all sheets to process stuff
For Each sht In Worksheets
ProcessData sht
Next sht
' Free up memory assigned to collections
For Each col In MasterCollection
Set col = Nothing
Next col
Set MasterCollection = Nothing
End Sub
Private Sub GetData(ASheet As Worksheet)
' Get data from one sheet:
' save range names in a collection
' add collection to MasterCollection
Dim oCollection As New Collection
Dim MyRange As Range
Dim FirstAddress As String
Dim arrSearchWords As Variant
Dim i As Integer
arrSearchWords = Array("blahblahblah", "bleepbleepbleep", _
"foofoofoo", "etc.etc.etc")
With ASheet
For i = LBound(arrSearchWords) To UBound(arrSearchWords)
Set MyRange = .UsedRange.Find(what:=arrSearchWords(i))
If Not MyRange Is Nothing Then
FirstAddress = MyRange.Address
Do
'add this sheet to my collection since it found something
oCollection.Add MyRange.Address
Set MyRange = .UsedRange.FindNext(MyRange)
Loop While Not MyRange Is Nothing _
And MyRange.Address <> FirstAddress
End If
Next i
If oCollection.Count > 0 Then
MasterCollection.Add oCollection, .Name
Else
Set oCollection = Nothing
End If
End With
End Sub
Private Sub ProcessData(ASheet As Worksheet)
' Process data in one sheet:
' Collection contains range names to process
Dim oCollection As Collection
Dim i As Integer
With ASheet
Set oCollection = MasterCollection(.Name)
For i = 1 To oCollection.Count
' Demonstrate accessing data from the collection
With .Range(oCollection(i))
MsgBox ASheet.Name & "!" & .Address & ": " & .Value
End With
Next i
End With
End Sub
On Error Resume Next
Set oCollection = MasterCollection(.Name)
ProcessData