VBA Excel 来自其他工作簿的插入匹配函数

VBA Excel insert-match function from other workbook

我正在尝试在 for 循环中实现索引匹配功能。我想将值从其他工作簿复制到活动单元格工作簿。

这是我写的代码:

Sub Amazon_Index()
Dim WB As Workbook
Dim Partner As Worksheet
Dim AcWS As Worksheet

Dim LastRow As Long
    With ActiveSheet
        LastRow = .Range("A1").SpecialCells(xlCellTypeLastCell).Row
    End With


Set WB = Workbooks.Open(Filename:="C:\Users\Adis\Desktop\Amazon narudžbe\Partner lista\partner.xlsx")
Set Partner = WB.Sheets("lista")



For i = 2 To LastRow
  ActiveSheet.Cells(i, 19) = _
  Application.WorksheetFunction.Index(Sheets("Partner").Range("A1:E100"), _
       Application.WorksheetFunction.Match(ActiveSheet.Cells(i, 18), _
       Sheets("Partner").Range("B1:B100"), 0), 5)
Next

End Sub

请尝试更换:

For i = 2 To LastRow
  ActiveSheet.Cells(i, 19) = _
  Application.WorksheetFunction.Index(Sheets("Partner").Range("A1:E100"), _
       Application.WorksheetFunction.Match(ActiveSheet.Cells(i, 18), _
       Sheets("Partner").Range("B1:B100"), 0), 5)
Next

与:

dim mtch
For i = 2 To LastRow
  mtch = Application.WorksheetFunction.Match(ActiveSheet.Cells(i, 18).value , _
         Sheets("Partner").Range("B1:B100"), 0)
  If not IsError(mtch) then
     ActiveSheet.Cells(i, 19) = _
     Application.WorksheetFunction.Index(Sheets("Partner").Range("A1:E100"), mtch, 5)
   End If                        
Next

如果 Application.WorksheetFunction.Match(... 没有找到匹配项,则会引发错误...

不同的方法,但它有效。

子Insert_Index() ' ' Insert_Index 宏 ' 昏暗的 LastRow 一样长 使用 ActiveSheet LastRow = .Range("A1").SpecialCells(xlCellTypeLastCell).Row 结束于

Range("S2").Select
ActiveCell.FormulaR1C1 = _
    "=INDEX([partner.xlsx]lista!R1C1:R70C5,MATCH(RC[-1],[partner.xlsx]lista!R1C2:R70C2,0),1)"
  Range("T2").Select
ActiveCell.FormulaR1C1 = _
    "=INDEX([partner.xlsx]lista!R1C1:R70C5,MATCH(RC[-2],[partner.xlsx]lista!R1C2:R70C2,0),5)"
    Range("S2:T2").Select
Selection.AutoFill Destination:=Range("S2:T" & LastRow), Type:=xlFillDefault
    

结束子