VBA:两个工作簿之间的VLookup

时间:2015-06-03 15:53:32

标签: excel vba excel-vba combobox

我想知道是否有人可以帮助我。我创建了一个包含3个组合框的Userform。 Combobox 1和2列出了所有打开的工作簿。 Combobox 3列出了Combobox 2的工作表。我现在想要运行Vlookup。查找值是从D9开始到最后一个单元格的每个单元格中的值(在这种情况下为产品代码),其值为Combobox2的第一个工作表的D列。查找范围将是(" A5:S ###" [行数根据文件而变化]")。 Vlookup公式应该在Combobox2的第一个工作表的第一列中,从" I9"开始。循环遍历I9中的每个单元,直到查找D9中的所有代码。 我不断收到错误主要是“运行时错误' 9'”:下标超出范围。这是我的代码。

Option Explicit

Private Sub CancelButton_Click()
    Stopped = True
    Unload Me
End Sub

Private Sub ComboBox1_Change()
Dim ScheduleA As Workbook
Dim Termset As Worksheet
Set ScheduleA = Workbooks(Me.ComboBox1.Value)
  With Me.ComboBox3
  For Each Termset In ScheduleA.Worksheets
            .AddItem Termset.Name
    Next Termset
    End With
End Sub


Private Sub FillACDButton_Click()
    Dim ACDRebateInfo As Worksheet
    Dim lastRow As Long
    Dim NewRebate As Single
    Dim NewRebateType As String
    Dim LookUp_Range As Range
    Dim ActionCode As String
    Dim ACD_NewRebate As Range
    Dim ACD_NewRebateType As Range
    Dim ACD_ActionCode As Range
    Dim SCC As Range
    Dim Cell As Range
    Set ACDRebateInfo = Workbooks(Me.ComboBox2.Value).Worksheets(1)
    Set ACD_NewRebate = ACDRebateInfo.Range("I9:I500")
    Set ACD_NewRebateType = ACDRebateInfo.Range("J9:J500")
    Set ACD_ActionCode = ACDRebateInfo.Range("B9:B500")
    Set LookUp_Range = Worksheets(Me.ComboBox3.Value).Range("A5:S400")
    Set SCC = ACDRebateInfo.Range("D9:D230")
    With ACDRebateInfo
    For Each Cell In ACD_ActionCode
    ActionCode = Application.WorksheetFunction.VLookup(SCC, LookUp_Range, 17, False)
    Next Cell
    End With
    Unload Me
End Sub



Private Sub UserForm_Initialize()
    Dim wkb As Workbook
        For Each wkb In Application.Workbooks
            Me.ComboBox1.AddItem wkb.Name
            Me.ComboBox2.AddItem wkb.Name
    Next wkb
End Sub

1 个答案:

答案 0 :(得分:0)

不确定这是你的问题,但这段代码没有意义:

For Each Cell In ACD_ActionCode
    ActionCode = Application.WorksheetFunction.VLookup(SCC, LookUp_Range, 17, False)
Next Cell

您正在循环操作代码,但未使用Cell变量