简体   繁体   English

Excel VBA数组数组

[英]Excel VBA Array of Arrays

I am trying to create an array of arrays inside of the macros of Excel. 我正在尝试在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? 我知道我可以拥有一个2D阵列,但是如何跟踪哪个阵列插槽是打开的呢? I have this variable (the 12 is the months, the 16 is the total number of dates allowed). 我有这个变量(12是月份,16是允许的日期总数)。

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 ... 理想情况下,我会将所有9月份的月份存储在monthDates(9)或类似的文件中,但是我却迷失了……

  • 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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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