簡體   English   中英

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

我知道我可以擁有一個2D陣列,但是如何跟蹤哪個陣列插槽是打開的呢? 我有這個變量(12是月份,16是允許的日期總數)。

Dim monthlyDates(12, 16) As Variant

理想情況下,我會將所有9月份的月份存儲在monthDates(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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM