【问题标题】:Lookup values from another sheet and paste results as values从另一个工作表中查找值并将结果粘贴为值
【发布时间】:2021-11-05 13:22:33
【问题描述】:

我相信这是简单的编码,但我自己无法处理。我尝试使用宏记录器,但我无法获得想要的结果......我不擅长 VBA 编码:/ 我有两个工作表,Sheet1 和 Sheet2。在 ws1 我有 A、B、C 列,我在其中输入从第 10 行开始的数据。我的源数据在 A:J 范围内的 ws2 中。我需要一个宏来搜索来自 ws1 Range B10: x (lastrow) in ws2 in column B 的值,并将搜索值从 ws2 中的 A 列返回到 ws1 中的 A 列,并将 G 列从 ws2 返回到 ws1 中的 C 列. 标准的 VlookUp/Xlookup 函数可以完成这项工作,但我需要将 ws1 中的 A、B、C 列中的值作为值,因为如果有函数,我的其他代码将无法工作。 ws1 列 B 中的值是在条件下从 ws2 中的 B 列生成的值。所以行号是从第 10 行到 X。

提前致谢!

我尝试过的函数,但导致我的其他代码停止工作。

单元格 A10 函数:=XLOOKUP(Sheet1!B10;Sheet2!B:B;Sheet2!A:A)

单元格 C10 函数:=XLOOKUP(Sheet1!B10;RSheet2!B:B;Sheet2!G:G)

【问题讨论】:

  • 您的数据样本和预期结果将帮助我们了解您的问题。

标签: excel vba


【解决方案1】:
Option Explicit
Sub mylookup()

    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim cell As Range, iLastRow As Long
    Dim r, ar
    Set ws1 = Sheets("Sheet1")
    Set ws2 = Sheets("Sheet2")

    ' sheet2
    iLastRow = ws2.Cells(Rows.Count, "B").End(xlUp).Row
    ar = ws2.Range("B1:B" & iLastRow)

    ' sheet1
    iLastRow = ws1.Cells(Rows.Count, "B").End(xlUp).Row
    For Each cell In ws1.Range("B10:B" & iLastRow)
        r = Application.Match(cell, ar, 0)
        If Not IsError(r) Then
            cell.Offset(0, -1) = ws2.Cells(r, "A")
            cell.Offset(0, 1) = ws2.Cells(r, "G")
        End If
    Next
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2017-12-03
    • 2020-09-30
    • 1970-01-01
    相关资源
    最近更新 更多