在VBA中处理公式结果到Vlookup

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

我的 Excel 电子表格中有一个表格,可以从外部电子表格中提取所有数据。我创建了一个表单,该表单应将数据提取到显示与产品相关的所有详细信息的表单中。在组合框中输入产品编号,然后在表中使用 Vlookup 进行搜索。当我键入产品编号(例如看起来像 FC222555)时,按下搜索按钮后,我收到运行时错误 1004 无法获取工作表函数类的 Vlookup 属性。 我发现由于单元格中的公式,Vlookup 找不到这个数字,我需要转换公式结果,以便 Vlookup 可以找到它。请看代码:

Private Sub CommandButton1_Click()
'variables
Dim Internal_names As String
Dim Prdd As Date
Dim Vn As String
Dim Sh1 As String
Dim Ln As Long
Dim Fx As Long
Dim Pds As String
Dim Pdd As Date
Dim Iss As String
Dim Cms As String
Dim Cmd As Date
Dim Shs As String
Dim Shd As Date
Dim Com As String
Dim x As String

'combobox value vlookup
With ThisWorkbook.Worksheets("Delivery")
Set Rng = Range("A:Y")
Prdd = Application.WorksheetFunction.VLookup(ComboBox1.Value, Rng, 21, False)
Vn = Application.WorksheetFunction.VLookup(ComboBox1.Value, Rng, 15, False)
Sh1 = Application.WorksheetFunction.VLookup(ComboBox1.Value, Rng, 1, False)
Ln = Application.WorksheetFunction.VLookup(ComboBox1.Value, Rng, 2, False)
Fx = Application.WorksheetFunction.VLookup(ComboBox1.Value, Rng, 3, False)
Pds = Application.WorksheetFunction.VLookup(ComboBox1.Value, Rng, 4, False)
Pdd = Application.WorksheetFunction.VLookup(ComboBox1.Value, Rng, 5, False)
Iss = Application.WorksheetFunction.VLookup(ComboBox1.Value, Rng, 6, False)
Cms = Application.WorksheetFunction.VLookup(ComboBox1.Value, Rng, 9, False)
Cmd = Application.WorksheetFunction.VLookup(ComboBox1.Value, Rng, 10, False)
Shs = Application.WorksheetFunction.VLookup(ComboBox1.Value, Rng, 11, False)
Shd = Application.WorksheetFunction.VLookup(ComboBox1.Value, Rng, 12, False)
Com = Application.WorksheetFunction.VLookup(ComboBox1.Value, Rng, 14, False)

'text boxes value
dateprod.Value = Prdd
vin.Value = Vn
shift.Value = Sh1
line.Value = Ln
fixture.Value = Fx
pdishift.Value = Pds
pdidate.Value = Pdd
details.Value = Iss
cmmshift.Value = Cms
cmmdate.Value = Cmd
shipshift.Value = Shs
shipdate.Value = Shd
comments.Value = Com


End With
End Sub

我试图让变量来处理公式结果,但后来我收到了无效的限定符错误。


Dim x As String

With ThisWorkbook.Worksheets("Delivery")
x = ComboBox1.Value
Prdd = Application.WorksheetFunction.VLookup(x.Value, Rng, 21, False)
Vn = Application.WorksheetFunction.VLookup(x.Value, Rng, 15, False)
Sh1 = Application.WorksheetFunction.VLookup(x.Value, Rng, 1, False)
Ln = Application.WorksheetFunction.VLookup(x.Value, Rng, 2, False)
Fx = Application.WorksheetFunction.VLookup(x.Value, Rng, 3, False)

您能帮我了解我需要做什么才能使其发挥作用吗?

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

根据我上面的评论:

Private Sub CommandButton1_Click()
    
    Dim m As Variant, rw As Range, ws As Worksheet, v
    
    Set ws = ThisWorkbook.Worksheets("Delivery")
    
    v = ComboBox1.Value
    m = Application.Match(v, ws.Columns("A"), 0) 'try to find matched row
    
    If Not IsError(m) Then  'got a match?
        Set rw = ws.Rows(m)
        dateprod.Value = rw.Cells(21).Value
        vin.Value = rw.Cells(15).Value
        shift.Value = rw.Cells(1).Value
        Line.Value = rw.Cells(2).Value
        'etc etc
    Else
        MsgBox "No match found for '" & v & "'!", vbExclamation
    End If
   
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.