Excel VBA 数组的数组
Excel VBA Array of Arrays
我正在尝试在 Excel 的宏中创建一个数组数组。这是我的问题......我正在创建一个年历并想突出显示该日历中的日期。
我在工作表中有一个日期范围。这些可以是我想记住的任何类型的日期,等等。我读入它们然后创建日历并将这些不同的日期设置为不同的背景颜色。
9/24/2015
1/20/2015
4/5/2015
9/30/2015
1/1/2015
在我有限的思维中,我会阅读它们,按月分组(年份无关紧要),然后将日期与该月相关联。
9 -> 24, 30
1 -> 20, 1
4 -> 5
这是我目前所拥有的
'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
我知道我可以有一个二维数组,但我如何跟踪哪个数组槽是打开的?我有这个变量(12 是月份,16 是允许的日期总数)。
Dim monthlyDates(12, 16) As Variant
理想情况下,我会将所有 9 月的月份存储在 monthlyDates(9) 或类似的东西中,但我不知道...
- 存储时如何跟踪?
- 如何在创建特定月份时访问和循环这些值?
有什么想法吗?
如果我没理解错的话,我觉得这个选项适合你...
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
结果
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
我正在尝试在 Excel 的宏中创建一个数组数组。这是我的问题......我正在创建一个年历并想突出显示该日历中的日期。
我在工作表中有一个日期范围。这些可以是我想记住的任何类型的日期,等等。我读入它们然后创建日历并将这些不同的日期设置为不同的背景颜色。
9/24/2015
1/20/2015
4/5/2015
9/30/2015
1/1/2015
在我有限的思维中,我会阅读它们,按月分组(年份无关紧要),然后将日期与该月相关联。
9 -> 24, 30
1 -> 20, 1
4 -> 5
这是我目前所拥有的
'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
我知道我可以有一个二维数组,但我如何跟踪哪个数组槽是打开的?我有这个变量(12 是月份,16 是允许的日期总数)。
Dim monthlyDates(12, 16) As Variant
理想情况下,我会将所有 9 月的月份存储在 monthlyDates(9) 或类似的东西中,但我不知道...
- 存储时如何跟踪?
- 如何在创建特定月份时访问和循环这些值?
有什么想法吗?
如果我没理解错的话,我觉得这个选项适合你...
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
结果
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