這裏是VBA函數填充具有一套獨特幾個月的數組,從起始月份和結束月份產生:如何從當前函數VBA Excel中獲取單元行
Function get_months(matrix_height As Integer) As Variant
Worksheets("Analysis").Activate
Dim date_range As String
Dim column As String
Dim uniqueMonths As Collection
Set uniqueMonths = New Collection
Dim dateRange As range
Dim months_array() As String 'array for months
column = Chr(64 + 1) 'A
date_range = column & "2:" & column & matrix_height
Set dateRange = range(date_range)
On Error Resume Next
Dim currentRange As range
For Each currentRange In dateRange.Cells
If currentRange.Value <> "" Then
Dim tempDate As Date: tempDate = CDate(currentRange.Text) 'Convert the text to a Date
Dim parsedDateString As String: parsedDateString = Format(tempDate, "MMM-yyyy")
uniqueMonths.Add Item:=parsedDateString, Key:=parsedDateString
End If
Next currentRange
On Error GoTo 0 'Enable default error trapping
'Loop through the collection and view the unique months and years
Dim uniqueMonth As Variant
Dim counter As Integer
counter = 0
For Each uniqueMonth In uniqueMonths
ReDim Preserve months_array(counter)
months_array(counter) = uniqueMonth
Debug.Print uniqueMonth
counter = counter + 1
Next uniqueMonth
get_months = months_array
End Function
如何操作此函數返回要添加到我的months數組中的每個值的單元格行。
什麼是存儲這兩個值即日期(OCT-2011)&行號(即456)
拖陣列的最佳方式?然後返回一個包含這兩個數組的數組?
任何人都可以提供解決這個問題嗎?
BirdsView:你可以使用一個二維數組,而不是2數組? –
這是一個函數,設計用於從VBA中的工作表或其他函數中調用? – Bathsheba
@Bathsheba從另一個函數來看,它的名字叫做main()sub – cwiggo