模块中的第二个Vlookup拉错列

时间:2018-03-22 07:53:40

标签: excel vba excel-vba vlookup

我在同一个模块中使用第二个vlookup但有一个不同的子例程时遇到问题。以下是第一个子例程

中的代码
Sub ImportSchedUnschedData()

Dim combinedBook As Workbook
Dim filter As String
Dim caption As String
Dim combinedFilename As String
Dim combinedWorkbook As Workbook
Dim targetWorkbook As Workbook

MsgBox ("Select BRAM Report")

Set targetWorkbook = ThisWorkbook

filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file"
combinedFilename = Application.GetOpenFilename(filter, , caption)

Set combinedWorkbook = Application.Workbooks.Open(combinedFilename)

Dim lastRow As Long

With ThisWorkbook.Worksheets("Input Sched-Unsched Split")

    lastRow = .Cells(.Rows.Count, "M").End(xlUp).Row

    .Range("I15:I" & lastRow).FormulaR1C1 = _
                       "=VLOOKUP(RC[-8],'[" & combinedWorkbook.Name & "]Sheet1'!R1C1:R700000C2,2,0)"

    combinedWorkbook.Close False

End With

End Sub

我尝试使用的第二个vlookup是

Sub ImportRepurchaseData()

Dim combinedBook As Workbook
Dim filter As String
Dim caption As String
Dim combinedFilename As String
Dim combinedWorkbook As Workbook
Dim targetWorkbook As Workbook

MsgBox "Select Loss Information File"

Set targetWorkbook = ThisWorkbook

filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file"
combinedFilename = Application.GetOpenFilename(filter, , caption)

Set combinedWorkbook = Application.Workbooks.Open(combinedFilename)  

Dim lastRow As Long

With ThisWorkbook.Worksheets("Input List Repurch")

    lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row

    .Range("D9:D" & lastRow).FormulaR1C1 = _
                       "=VLOOKUP(RC[-2],'[" & combinedWorkbook.Name & "]Sheet1'!R1C1:R700000C2,2,0)"

    combinedWorkbook.Close False

End With

End Sub

我遇到的问题是当我运行第二个vlookup列时,我就是这样做的。在我的"输入列表Repurch"选项卡也正在填充vlookup。我在第二个vlookup中的目标如下:

查找值将是ThisWorkbook中的B9(带有"输入列表重新编辑的工作簿"选项卡)。 Table Array将在combinedWorkbook中为A1:B100000。 Col索引号将为2,范围查找为0.应将公式一直拖到M列中的结束数据。

任何帮助都会受到赞赏吗?

非常感谢。

0 个答案:

没有答案