RawlinsCross
Active Member
- Joined
- Sep 9, 2016
- Messages
- 437
I have a spreadsheet (clearly) that has rows of numbers under a four categories ("Active", "Lost", Recovered", "Damaged"). These four categories repeat several dozen times across the column space. I want to iterate over the range and save all the individual numbers in all the columns that read each category in an array. The # of rows is variable so I figured I use a collection. But where I have four categories, I figured I'd use a dictionary to store each collection of numbers corresponding to each category. (Hope that was clear).
This is what I've tried but not having much luck. I'm just trying to get it started with the "Active" category and then apply it to the rest.
Class Code
Module Code
This is what I've tried but not having much luck. I'm just trying to get it started with the "Active" category and then apply it to the rest.
Class Code
VBA Code:
Option Explicit
Public Active As Collection
Public Lost As Collection
Public Recovered As Collection
Public Damaged As Collection
Module Code
VBA Code:
Private Sub MakeDictionary()
Dim mlFirstRow As Long, mlLastRow As Long
Dim i As Long, j As Long
Dim sCat As String
Dim C As Collection
Dim moTable As clsTable
Dim mwSht As Worksheet
Dim mrRange As Range
Dim mvMain As Variant
'Testing
mlFirstRow = 2
mlLastRow = 23
Set moTableDict = CreateObject("Scripting.Dictionary")
Set mwSht = ThisWorkbook.Worksheets("Main")
Set mrRange = mwSht.Range("rngMain")
mvMain = mrRange.value
For i = mlFirstRow + 8 To mlLastRow + 8 '8 to account for the header rows in the spreadsheet
For j = 2 To UBound(mvMain, 2)
sCat = mvMain(1, j) 'Header column (four categories that repeat)
With moTableDict
If Not .Exists(sCat) Then
Set moTable = New clsTable
.Add sCat, moTable
If sCat = "Active" Then
Set C = New moTable.Active
If IsNumber(mvMain(i, j)) Then
C.Add mvMain(i, j)
End If
End If
Else
Set moTable = moTableDict(sCat)
If sCat = "Active" Then
Set C = moTable.Active
If IsNumber(mvMain(i, j)) Then
C.Add mvMain(i, j)
End If
End If
End If
End With
Next j
Next i
Set moTable = moTableDict("Active")
Set C = moTable.Active
Stop
End Sub