If Left的错误9

时间:2018-02-02 09:31:44

标签: excel vba excel-vba

我正在运行错误时间9,我不知道为什么。一些信息是:

表1是特殊客户

工作表2使用发票复制PDF

Public Sub TestMe()

Dim r1      As Variant

Dim rData   As Variant
Dim r       As Variant
Dim result  As Variant

Dim i       As Integer

rData = Application.Transpose(Worksheets(2).Range("A:A"))
r1 = Application.Transpose(Worksheets(1).Range("C2:C33"))

For Each r In r1
  result = Application.Match(r, rData, 0)  ' returns row of matching customer
  If Not IsError(result) Then  ' found customer, now look for "418"
    For i = 1 To 4
      If Left(Trim(rData(result - i, 3)), 1) = "418" Then
        MsgBox rData(result - i, 1)
      End If
    Next
  End If
Next r

End Sub

所以我在If Left(Trim(rData(result - i, 3)), 1) = "418" Then收到此错误。我正在尝试从rData获取发票编号,该编号以418开头,其中包含客户发票的PDF和r1是有特殊需求的客户(最多r50 {1}})。问题是发票nr比找到的客户名称(r)高1到4个单元。

1 个答案:

答案 0 :(得分:1)

rData是一维数组:

rData = Application.Transpose(Worksheets(2).Range("A:A"))

您正在尝试获取一维数组的2.维度。它会给你一个错误:

rData(result - i, 3)