简体   繁体   中英

Excel VBA Array of Arrays

I am trying to create an array of arrays inside of the macros of Excel. Here is my problem... I am creating a year calendar and want to highlight dates inside that calendar.

I have a range of dates in a worksheet. These would be any type of dates I want to remember, etc. I read them in and then create the calendar and make these a different dates a different background color.

9/24/2015
1/20/2015
4/5/2015
9/30/2015
1/1/2015

In my limited thinking I would read them in, Group them by month (year doesn't matter) and then put the dates associated with that month.

9 -> 24, 30
1 -> 20, 1
4 -> 5

Here is what I have so far

'Set Variables
Dim ImportantDays As Variant
Dim id As Integer
Dim tempSplitDateArray() As Integer

'Grab the dates from the entered WorkSheet
ImportantDays = Worksheets("MainData").Range("E4:E19")

'Loop through the dates entered
For id = LBound(ImportantDays, 1) To UBound(ImportantDays, 1)
    If ImportantDays(id, 1) <> "" Then
        tempSplitDateArray() = Split(ImportantDays(id, 1), "/")
        '--I now have tempSplitDateArray(0) = month
        '--tempSplitDateArray(1) = day

        '------------------------------------
        '-- Not sure of my next step here
        '------------------------------------
    End If
Next id

I know I can have a 2D array, but how do I keep track of which array slot is open? I have this variable (the 12 is the months, the 16 is the total number of dates allowed).

Dim monthlyDates(12, 16) As Variant

Ideally I would store all the September months in monthlyDates(9) or something like that, but I am at a loss as to ...

  • How to keep track when storing them?
  • How to access and loop through the values when that particular month is being created?

Any thoughts?

If I understand correctly, I think this option is right for you ...

Sub test()
Dim id&, z&, oCell As Range, Key, MKey
Dim I_Month As Object: Set I_Month = CreateObject("Scripting.Dictionary")
Dim I_Day As Object: Set I_Day = CreateObject("Scripting.Dictionary")
Dim Cnt As Object: Set Cnt = CreateObject("Scripting.Dictionary")
Dim Month_count As Object: Set Month_count = CreateObject("Scripting.Dictionary")
id = 1
'Grab the dates from the entered WorkSheet
For Each oCell In Worksheets("MainData").Range("E4:E19")
    I_Month.Add id, Month(oCell.Value)
    I_Day.Add id, Day(oCell.Value)
    id = id + 1
Next
id = 12
z = 0
While id <> 0
    For Each Key In I_Month
        If I_Month(Key) = id Then z = z + 1
    Next
    Cnt.Add id, z
    id = id - 1: z = 0
Wend
For Each Key In I_Month
        For Each MKey In Cnt
            If MKey = I_Month(Key) Then
                id = Cnt(MKey)
                Exit For
            End If
        Next
    Month_count.Add Key, id
Next
For Each Key In I_Month
   Debug.Print Key, I_Month(Key), I_Day(Key), Month_count(Key)
Next
End Sub

result

 Key           Month         Day           Count of the Month iteration
 1             6             22            4 
 2             10            24            2 
 3             6             15            4 
 4             10            28            2 
 5             1             14            3 
 6             1             9             3 
 7             11            15            1 
 8             1             24            3 
 9             6             2             4 
 10            3             21            1 
 11            12            26            2 
 12            5             25            2 
 13            2             23            1 
 14            12            7             2 
 15            5             31            2 
 16            6             5             4 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM