WorksheetFunction.Vlookup针对另一个工作簿运行时错误13

问题描述 投票:0回答:1

下面是我正在运行的代码。我在提到的行上收到“类型不匹配”错误。任何帮助将不胜感激。所有变量都是全局设置的。 “ Source_BK”实际上是该代码特定部分的目标书,“ Mvmt_Bk”工作簿和“ Mvmt_Sht”工作表将目标vlookup数据保存在“ H4:I&Mvmt_LR”范围内(最后一行)。似乎“ VL_Srch_Rng”没有带入vlookup函数中。

Dim Source_Bk As Workbook, VL_Rslt As Range, VL_Find As String, VL_Srch_Rng As Variant, Mvmt_LR As Long
'----------
Mvmt_Bk.Activate
Mvmt_Sht.Activate
With ActiveSheet
   Range("H:H").Insert Shift:=xlToLeft
   For o = 4 To Cells(Rows.Count, 2).End(xlUp).Row
      Range("H" & o).Value = Range("A" & o) & "." & Range("C" & o).Value
   Next o
   Range("H3").Value = "Lookup Value"
   Range("H4").ColumnWidth = 14
   Range("H:H").NumberFormat = "000.000000000000"
   Mvmt_LR = .Cells(Rows.Count, 2).End(xlUp).Row
 '**** Edited to show updated attempt
   Set VL_Srch_Rng = Mvmt_Sht.Range("$H$4:$I$" & Mvmt_LR)
End With

Source_Bk.Activate
Dist_Sht.Activate
With Dist_Sht
   For u = Dist_LR To 2 Step -1
      If Range("J" & u).Value = "0" Then
         Range("J" & u).EntireRow.Delete
      Else
      For s = 12 To Dist_LC
         If Cells(u, s).Value <> "0" Then
            Cells(u, s).Value = Mid(Cells(1, s), Len(Cells(1, s)) - 7, 3) & "." & Cells(u, 2)
            Cells(u, s).NumberFormat = "000.000000000000"
            Cells(u, s).Value = Cells(u, s).Value
         End If
      Next s
      End If
   Next u

   For v = 2 To Dist_LR
      For w = 12 To Dist_LC
         If Cells(v, w) <> 0 Then
            VL_Find = Cells(v, w).Value
'****This next line throws the error, below it is another attempt:
'**** Edited to show updated attempt
            VL_Rslt = Application.WorksheetFunction.VLookup(VL_Find, 
                VL_Srch_Rng, 2, False)
            Dist_Sht.Cells(v, w).Value = VL_Rslt
         End If
      Next w
   Next v

End With
excel vba worksheet-function
1个回答
0
投票

您可以尝试使用For循环并重新创建VLOOKUP将执行的操作。我相信以下代码应该是一个好的开始:

Dim cell as Variant

For v = 2 To Dist_LR
    For w = 12 To Dist_LC
        If Cells(v, w) <> 0 Then
            VL_Find = Cells(v, w).Value

            For Each cell in VL_Srch_Rng                    'check each cell in the search range
                If cell.Value = VL_Find Then                'if the cell is the same as VL_Find, yay
                    VL_Rslt = Mvmt_Sht.Cells(cell.Row,"I")  'This line may need fiddling.
                End If
            Next cell 

        End If
    Next w
Next v
© www.soinside.com 2019 - 2024. All rights reserved.