ThisWorkbook生成运行时错误9:下标超出范围

时间:2017-11-02 19:26:55

标签: excel vba excel-vba

这个(从数组名称)的基本思想是时间表。

我有一张主时间表,上面有每个人的名字,+日期。

概念是,从L / UBounds of array打开http链接。从特定工作表中获取数据,将其放入我的主工作表中,关闭该工作簿,然后转到下一个名称。

Sub PassToProc()
Dim arr(4) As String
arr(0) = "https://sharepoint/Time Sheets/Jeff.xlsm"
arr(1) = "https://sharepoint/Time Sheets/Jonathan.xlsm"
arr(2) = "https://sharepoint/Time Sheets/Jim.xlsm"
arr(3) = "https://sharepoint/Time Sheets/Topher.xlsm"
arr(4) = "https://sharepoint/Time Sheets/Brandon.xlsm"
' Pass the array to function
UseArray arr
End Sub

Function UseArray(ByRef arrs() As String)
Dim name As String
Dim i As Integer

For i = LBound(arrs) To UBound(arrs)

    If Cells(3, i + 4) <> "" Then
        name = Cells(3, i + 4)

        Select Case name

            Case "Jeff"
                Call InfoPuller(arrs, 0)

            Case "Jonathan"
                Call InfoPuller(arrs, 1)

            Case "Jim"
                Call InfoPuller(arrs, 2)

            Case "Topher"
                Call InfoPuller(arrs, 3)

            Case "Brandon"
                Call InfoPuller(arrs, 4)

        End Select

    End If

Next i

End Function

Sub InfoPuller(ByRef link() As String, i As Integer)

Dim Pay1 As String, Pay2 As String, PayPeriod As String
Dim wkb As Workbook

Pay1 = Format(Cells(3, 2), "m-dd")
Pay2 = Format(Cells(24, 1), "m-dd")
PayPeriod = "" & Pay1 & " -- " & Pay2 & ""

'Jeff   (x,4) i = 0
'Jon    (x,5) i = 1
'Jim    (x,6) i = 2
'Topher (x,7) i = 3
'Brandon   (x,8) i = 4

Set wkb = Workbooks.Open(link(i))
' Pay period 1 Fill in

For j = 5 To 11
    ThisWorkbook.Sheets(PayPeriod).Cells(j, i + 4) = Workbooks.Open(link(i)).Worksheets(PayPeriod).Cells(j + 4, 9).Value
Next j

' Mileage / Bridge Week 1

Dim Mileage As Integer, Bridge As Integer, Store As Integer, Store2 As Integer
Dim MandB As String


Mileage = Workbooks.Open(link(i)).Worksheets(PayPeriod).Cells(28, 3).Value
Bridge = Workbooks.Open(link(i)).Worksheets(PayPeriod).Cells(29, 3).Value
MandB = Mileage & " / " & Bridge

ThisWorkbook.Sheets(PayPeriod).Cells(13, i + 4) = MandB

Store = Mileage
Store2 = Bridge

'Pay Period 2 Fill in
For j = 18 To 24
    ThisWorkbook.Sheets(PayPeriod).Cells(j, i + 4) = Workbooks.Open(link(i)).Worksheets(PayPeriod).Cells(j + 1, 9).Value
Next j

' Mileage / Bridge Week 2

Mileage = Workbooks.Open(link(i)).Worksheets(PayPeriod).Cells(28, 4).Value
Bridge = Workbooks.Open(link(i)).Worksheets(PayPeriod).Cells(29, 4).Value
MandB = Mileage & " / " & Bridge

ThisWorkbook.Sheets(PayPeriod).Cells(26, i + 4) = MandB

Store = Store + Mileage
Store2 = Store2 + Bridge

MandB = Store & " / " & Store2

ThisWorkbook.Sheets(PayPeriod).Cells(31, i + 4) = MandB

wkb.Close SaveChanges:=True

End Sub

问题在于此代码段。

For j = 5 To 11
ThisWorkbook.Sheets(PayPeriod).Cells(j, i + 4) = Workbooks.Open(link(i)).Worksheets(PayPeriod).Cells(j + 4, 9).Value
 Next j

ThisWorkbook是指宏正在运行的MasterSheet。 PayPeriod由日期决定。 Workbooks.Open(链接(i)...是因为如果我尝试工作簿(链接(i)),它不喜欢它(但它只打开1个副本,所以我不担心重复)。< / p>

一旦执行Thisworkbook.sheets(PayPeriod),我就会点击RTE-9下标超出范围错误。细胞排成一行,我的拉数据是正确的。我将https链接简化为Sharepoint以保护隐私。

0 个答案:

没有答案