简体   繁体   English

在另一个工作表中查找单元格值

[英]Find cell value in another sheet

I have to excel sheet and, using vba, I would like to get a value in one of those, search it in the other one and return a correspondant value in the first sheet. 我必须使用Excel,然后使用vba在其中一个中获取一个值,在另一个中搜索它,然后在第一张表中返回一个对应的值。

Given the sheet 1: 鉴于工作表1:

在此处输入图片说明

I would like to search the string I inserted in A5 in another sheet: 我想在另一张纸上搜索我在A5中插入的字符串:

Sheet 2: 工作表2:

在此处输入图片说明

Once I found the match (A2 in this case), I would get the 'value' (in D2 in this case) and report it in the cell B5 of the Sheet1. 一旦找到匹配项(在本例中为A2),我将获取“值”(在本例中为D2)并将其报告到Sheet1的单元格B5中。

That's what I tried: 那就是我尝试过的:

Dim rgFound As Range
Dim defVal As Range
Dim currParam As Range
Dim currParamDict As Range

For Each defVal In Range("B:B")

    Set currParam = Cells(Range(defVal).Row, Range(defVal).Column - 1)
    If currParam Is Nothing Then
        Debug.Print "Name was not found."        
    End If

    Set rgFound = Worksheets("Sheet2").Range("A:A").Find(currParam.value)
    If rgFound Is Nothing Then
        Debug.Print "Name was not found."
    Else
        Set currParamDict = Cells(Range(rgFound).Row, Range(rgFound).Column + 3)
        defVal.value = currParamDict.value
    End If
Next defVal

That's clearly wrong since the compiler gives me an error on Range at the line: 这显然是错误的,因为编译器在此行给我一个关于Range的错误:

Set currParam = Cells(Range(defVal).Row, Range(defVal).Column - 1)

Try this 尝试这个

Sub x()

Dim rgFound As Range
Dim defVal As Range
Dim currParam As Range
Dim currParamDict As Range

With Worksheets("Sheet1")
    For Each defVal In .Range("A2", .Range("A" & Rows.Count).End(xlUp)).Offset(, 1)
        Set currParam = defVal.Offset(, -1)
        If Len(currParam.Value) > 0 Then
            Set rgFound = Worksheets("Sheet2").Range("A:A").Find(currParam.Value)
            If rgFound Is Nothing Then
                Debug.Print "Name was not found."
            Else
                Set currParamDict = rgFound.Offset(, 3)
                defVal.Value = currParamDict.Value
            End If
        End If
    Next defVal
End With

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 双击单元格,找到另一张纸的价值 - Double click on cell and find value another sheet 确定单元格值以在另一张工作表中找到相同的值 - Determine cell value to find in another sheet the same value 在另一张纸上查找匹配值并粘贴到该行的下一个空单元格中 - Find matching value on another sheet and paste into next empty cell on that row 从一个单元格中查找值,从该单元格附近的单元格中复制值,然后将其粘贴到另一张纸上 - Find value from one cell, copy value from cell near to that cell and paste it to another sheet Excel VB宏从另一个工作表中查找值,然后更改当前工作表中的单元格值 - Excel VB Macro to find value from another sheet then change a cell value in current sheet 根据另一个工作表单元格值的值更改工作表中的单元格值 - Change a cell value in a sheet based on the value of another sheet cell value 如果工作表上的单元格值与另一张表上的单元格值相同,则删除该行 - Delete the row if the cell value on sheet is the same then the cell value on another sheet 找不到对象错误:循环将在另一个工作表中找到具有相同值的单元格,选择新找到的单元格的偏移量 - Object not found Error: loop that will find the cell with the same value in another sheet, select the offset of the new found cell 如何在Excel工作表中查找值,然后使用openpyxl在同一行的不同单元格中写入另一个值 - How to find a value in excel sheet and then write another value in a different cell in the same row with openpyxl Excel:值表1(在表2中查找)将值复制到单元格旁边 - Excel : Value Sheet 1 (Find at Sheet 2) Copy the Value next to the cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM