这是我的功能,即根据我的工作表Eng_Availability_Report
中的现有表格获取包含日期的数组并返回每周的小时数。对我来说,它似乎是正确的,但VBA向我发送了一条错误1004的消息。我使用MsgBox
返回周数,实际数字存在于我正在寻找的范围内。
Public Function ReturnHoursPerWeek(Arr1() As Variant) As Variant
Dim Hours() As Double, k As Integer, WeekNumber As Integer
Dim ws As Worksheet, wb As Workbook
k = 1
Set wb = ThisWorkbook
Set ws = wb.Sheets("Eng_Availability_Report")
If LBound(Arr1()) = UBound(Arr1()) Then
ReDim Hours(LBound(Arr1()))
WeekNumber = Int(((Arr1(1, 1) - DateSerial(Year(Arr1(1, 1)), 1, 0)) + 6) / 7)
MsgBox (" " & WeekNumber & " ")
If WeekNumber > 0 And WeekNumber < 14 Then
Hours(k) = Application.WorksheetFunction.VLookup(WeekNumber, ThisWorkbook.Sheets("Eng_Availability_Report").Range("F6:G19"), 7, False)
ElseIf WeekNumber > 14 And WeekNumber < 27 Then
Hours(k) = Application.WorksheetFunction.VLookup(WeekNumber, ThisWorkbook.Sheets("Eng_Availability_Report").Range("H6:I19"), 9, False)
ElseIf WeekNumber > 27 And WeekNumber < 40 Then
Hours(k) = Application.WorksheetFunction.VLookup(WeekNumber, ThisWorkbook.Sheets("Eng_Availability_Report").Range("J6:K19"), 11, False)
ElseIf WeekNumber > 40 And WeekNumber <= 53 Then
Hours(k) = Application.WorksheetFunction.VLookup(WeekNumber, ThisWorkbook.Sheets("Eng_Availability_Report").Range("L6:M19"), 13, False)
End If
Else
ReDim Hours(LBound(Arr1()) - UBound(Arr1()))
For i = LBound(Arr1()) To UBound(Arr1())
WeekNumber = Int(((Arr1(i, 1) - DateSerial(Year(Arr1(i, 1)), 1, 0)) + 6) / 7)
If WeekNumber > 0 And WeekNumber < 14 Then
Hours(k) = Application.WorksheetFunction.VLookup(WeekNumber, ThisWorkbook.Sheets("Eng_Availability_Report").Range("F6:G19"), 7, False)
ElseIf WeekNumber > 14 And WeekNumber < 27 Then
Hours(k) = Application.WorksheetFunction.VLookup(WeekNumber, ThisWorkbook.Sheets("Eng_Availability_Report").Range("H6:I19"), 9, False)
ElseIf WeekNumber > 27 And WeekNumber < 40 Then
Hours(k) = Application.WorksheetFunction.VLookup(WeekNumber, ThisWorkbook.Sheets("Eng_Availability_Report").Range("J6:K19"), 11, False)
ElseIf WeekNumber > 40 And WeekNumber <= 53 Then
Hours(k) = Application.WorksheetFunction.VLookup(WeekNumber, ThisWorkbook.Sheets("Eng_Availability_Report").Range("L6:M19"), 13, False)
End If
k = k + 1
Next
End If
ReturnHoursPerWeek = Hours()
End Function
答案 0 :(得分:0)
查看您的代码,当您只有2列(F:F-G:G)时,您似乎正在查找列号,例如7。我猜你需要改变范围或减少增量数字。
目前,您正在寻找阵列外的一列。通过字母你可能想要从根“匹配”单元格开始,然后转到右边的7/8/9等并返回值。我假设(通过代码)'匹配单元'在第一列中,因此通过更改范围使第一列始终为A,以您想要的列结束。这应该可以解决问题。