【问题标题】:Vlookup from another sheet and paste the result in another sheet从另一张纸上查找并将结果粘贴到另一张纸上
【发布时间】:2019-04-30 17:00:01
【问题描述】:

我在使用 vba 进行 vlookup 方面需要帮助,因为我无法在网上找到解决方案 情况是我有三张纸

  • 工作表 1:在单元格 B3 中查找具有名称的值
    Sheet 1

  • 工作表 2:包含 namesurname 列的查找表
    Sheet 2

  • 工作表 3:在单元格 B3 中查找值的结果为 surname
    Sheet 3

您可以参考图片以更好地理解

所以表格 1 中的值是我的查找值,姓氏必须打印在表格 3 中,表格数组在表格 2 中

我试过的代码是

Sub nameloopkup()
    Dim name As String
    Dim result As String
    Dim myrange As Range

    name = Worksheets("Sheet1").Range("B3").Value

    myrange = Worksheets("Sheet2").Range("A:B").Value
    result = Application.WorksheetFunction.VLookup(name, myrange, 2, False)
    ' the query does not run and i don't know how can i print the result in sheet 3
End sub

对于这里的许多人来说,这可能很简单。但考虑到我对 VBA 的熟悉程度,我需要一些相关的指导。

感谢任何形式的帮助或建议。

【问题讨论】:

  • “查询不运行”究竟是什么意思? ② 为什么不直接使用公式?我认为这里不需要 VBA。 ③ 你应该在myrange = Worksheets("Sheet2").Range("A:B").Value这一行得到一个错误,因为如果你的变量被声明为Range你必须使用Set myrange = Worksheets("Sheet2").Range("A:B")
  • 为什么不能在 Sheet 3 中直接使用 V-lookup 公式?您只需要 VBA 编码吗?我的建议,简单的方法是直接在 Sheet 3 中编写公式。
  • 是的,我需要通过 VBA 本身来执行此操作,因为它只是整个项目的一小部分。但是如何在表 3 中打印结果?任何建议
  • 是的,我需要通过 VBA 本身来执行此操作,因为它只是整个项目的一小部分。你对我有什么建议吗?

标签: excel vba


【解决方案1】:

其实你需要做的就是:

Sub nameloopkup()
    Dim Name As String
    Dim Result As String
    Dim SearchIn As Variant 'variant to use it as array

    Name = Worksheets("Sheet1").Range("B3").Value
    SearchIn = Worksheets("Sheet2").Range("A:B").Value 'read data into array

    On Error Resume Next 'next line errors if nothing was found
    Result = Application.WorksheetFunction.VLookup(Name, SearchIn, 2, False)
    On Error Goto 0

    If Result <> vbNullString Then
        Worksheets("Sheet3").Range("B3").Value = Result
    Else
        MsgBox "Nothing found"
    End If
End Sub

或者只写一个公式:

Sub NameLookUpFormula()
    Worksheets("Sheet3").Range("B3").Formula = "=VLOOKUP(Sheet1!B3,Sheet2!A:B,2,FALSE)"
End Sub

【讨论】:

    【解决方案2】:

    这是您可以做的 2... 有 2 个选项,如果您只需要 1 个数据条目,或者如果您需要一整组数据并每次从中挑选您需要的数据:

    Option Explicit
    Sub nameloopkup()
    
        Dim C As Range, LastRow As Long, EmptyRow As Long, i As Long, arrData
        Dim DictData As New Scripting.Dictionary 'You need to check Microsoft Scripting Runtime from references for this
        Dim wsNames As Worksheet, wsTable As Worksheet, wsSurnames As Worksheet
    
        'First thing, reference all your sheets
        With ThisWorkbook
            Set wsNames = .Sheets("Sheet1") 'change this as needed
            Set wsTable = .Sheets("Sheet2")
            Set wsSurnames = .Sheets("Sheet3")
        End With
    
        'Keep all the data in one dictionary:
        With wsTable
            LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'last row on Sheet2
            i = .Cells(1, .Columns.Count).End(xlToLeft).Column 'last column on Sheet2            
            arrData = .Range(.Cells(1, 1), .Cells(LastRow, i)).Value 'keep the data on the array
    
            'This will throw an error if there are duplicates
            For i = 2 To UBound(arrData)
                DictData.Add arrData(i, 1), i 'keep tracking of every name's position ' also change for arrData(i, 2) if you only need the surname
            Next i
        End With
    
        With wsNames
            LastRow = .Cells(.Rows.Count, 2).End(xlUp).Row 'last row on Sheet1
            For Each C In .Range("B3:B" & LastRow)
                EmptyRow = wsSurnames.Cells(wsSurnames.Rows.Count, 1).End(xlUp).Row
                wsSurnames.Cells(EmptyRow, 2) = DictData(C.Value) 'if you used arrData(i, 2) instead i
                wsSurnames.Cells(EmptyRow, 2) = arrData(DictData(C.Value), 2) 'If you used i
            Next C
        End With
    
    End Sub
    

    【讨论】:

      【解决方案3】:
      myrange = Worksheets("Sheet2").Range("A:B").Value
      result = Application.WorksheetFunction.VLookup(name, myrange, 2, False)
      

      这是您的错误。 Vlookup 的第二个参数是 Range,而不是 String。由于范围是一个对象,你还需要Set它:

      Set myrange = Worksheets("Sheet2").Range("A:B")
      result = Application.WorksheetFunction.VLookup(name, myrange, 2, False)
      

      【讨论】:

        猜你喜欢
        • 2020-06-12
        • 1970-01-01
        • 1970-01-01
        • 2017-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-02
        • 2023-02-02
        相关资源
        最近更新 更多