我正在尝试使用Excel查找全年的工作日。我需要它来创建时间表。问题是我不知道如何将多维数组传递给函数。在这种情况下是2D阵列。我得到弹出“类型不匹配”。有人有什么想法吗?
Function markWorkingDays(ByRef monthArray() As Integer)
Dim mainArray(1 To 12, 1 To 31) As Integer
For i = 1 To 12 'set all days as no-working (0 - False)
For j = 1 To 31
mainArray(i, j) = 0
Next j
Next i
For i = 1 To 12 'set first and last day of the month
firstDay = DateSerial(Year(Date), i, 1)
If i = 2 And Year(Date) And 400 = 0 Then 'if leap-year
lastDay = DateSerial(Year(Date), 0, monthArray(i))
Else
lastDay = DateSerial(Year(Date), i, monthArray(i))
End If
For j = 1 To 31 'set workings days as True (1)
If Weekday(firstDay) = 7 Or Weekday(firstDay) = 1 Then 'skip Saturday and Sunday to Monday
firstDay = firstDay + 1
Else
mainArray(i, j) = 1
firstDay = firstDay + 1
End If
If firstDay = lastDay Then
Exit For
End If
Next j
Next i
markWorkingDays = mainArray()
End Function
Function countWorkingDaysPerMonth(ByRef workingDaysArray() As Integer) '(ByRef workingDaysArray() As Integer)
Dim mainArray(1 To 12) As Integer
Dim counter As Integer
counter = 0
For i = 1 To 12
For j = 1 To 31
If workingDaysArray(i, j) = 1 Then
counter = counter + 1
End If
Next j
mainArray(i) = counter
counter = 0
Next i
End Function
Sub main()
Dim monthArray(0 To 12) As Integer
monthArray(0) = 29
monthArray(1) = 31
monthArray(2) = 28
monthArray(3) = 31
monthArray(4) = 30
monthArray(5) = 31
monthArray(6) = 30
monthArray(7) = 31
monthArray(8) = 31
monthArray(9) = 30
monthArray(10) = 31
monthArray(11) = 30
monthArray(12) = 31
Dim workingDaysArray() As Integer
workingDaysArray = markWorkingDays(monthArray())
Dim workingDaysPerMonthArray() As Integer
workingDaysPerMonthArray = countWorkingDaysPerMonth(workingDaysArray())
'display number of workings days in every month
For i = 1 To 12
std = workingDaysPerMonthArray(i) & " "
Next i
MsgBox std
MsgBox Total
End Sub
答案 0 :(得分:0)
您发布的代码有两个问题:
Function markWorkingDays(ByRef monthArray() As Integer)
的 As Integer()
强>
功能countWorkingDaysPerMonth()
未返回任何内容
Option Explicit
Public Function markWorkingDays(ByRef monthArray() As Integer) As Integer()
Dim mainArray(1 To 12, 1 To 31) As Integer, i As Integer, j As Integer
Dim firstDay As Variant, lastDay As Variant
For i = 1 To 12 'set all days as no-working (0 - False)
For j = 1 To 31
mainArray(i, j) = 0
Next j
Next i
For i = 1 To 12 'set first and last day of the month
firstDay = DateSerial(Year(Date), i, 1)
If i = 2 And Year(Date) And 400 = 0 Then 'if leap-year
lastDay = DateSerial(Year(Date), 0, monthArray(i))
Else
lastDay = DateSerial(Year(Date), i, monthArray(i))
End If
For j = 1 To 31 'set workings days as True (1)
If Weekday(firstDay) = 7 Or Weekday(firstDay) = 1 Then 'skip Sat, Sun, Mon
firstDay = firstDay + 1
Else
mainArray(i, j) = 1
firstDay = firstDay + 1
End If
If firstDay = lastDay Then
Exit For
End If
Next j
Next i
markWorkingDays = mainArray
End Function
Public Function countWorkingDaysPerMonth(ByRef workingDaysArray() As Integer) As Integer()
Dim mainArray(1 To 12) As Integer, i As Integer, j As Integer
Dim counter As Integer
counter = 0
For i = 1 To 12
For j = 1 To 31
If workingDaysArray(i, j) = 1 Then
counter = counter + 1
End If
Next j
mainArray(i) = counter
counter = 0
Next i
countWorkingDaysPerMonth = mainArray
End Function
Public Sub main()
Dim i As Integer, std As String
Dim monthArray(0 To 12) As Integer
monthArray(0) = 29
monthArray(1) = 31
monthArray(2) = 28
monthArray(3) = 31
monthArray(4) = 30
monthArray(5) = 31
monthArray(6) = 30
monthArray(7) = 31
monthArray(8) = 31
monthArray(9) = 30
monthArray(10) = 31
monthArray(11) = 30
monthArray(12) = 31
Dim workingDaysArray() As Integer
workingDaysArray = markWorkingDays(monthArray())
Dim workingDaysPerMonthArray() As Integer
workingDaysPerMonthArray = countWorkingDaysPerMonth(workingDaysArray())
'display number of workings days in every month
For i = 1 To 12
std = std & workingDaysPerMonthArray(i) & " "
Next i
MsgBox std
'MsgBox Total
End Sub
答案 1 :(得分:0)
另一种方法是使用变体类型:
Sub test2()
Dim x() As Variant
x = ActiveSheet.Range("A1:B3").Value
Call markWorkingDays(x)
End Sub
Function markWorkingDays(ByRef monthArray() As Variant)
Dim r As Integer
Dim c As Integer
For r = 1 To UBound(monthArray, 1)
For c = 1 To UBound(monthArray, 2)
MsgBox monthArray(r, c)
Next
Next
End Function
答案 2 :(得分:0)
我不确定我是否清楚自己,但问题就在这一行:
workingDaysPerMonthArray = countWorkingDaysPerMonth(workingDaysArray())
workingDaysArray()是返回二维数组的函数,我想将它作为参数传递给函数countWorkingDaysPerMonth()。而且有一个问题