简体   繁体   English

VBA Excel查找2个动态范围之间的值

[英]VBA Excel Look for values between 2 dynamic ranges

For each cell in 'myRange' I want to check for a range of values in Sheet2 and if the values in Sheet2 are found in myRange then for the corresponding row I want to put the value from Column A into Column E 对于'myRange'中的每个单元格,我要检查Sheet2中的值范围,如果在myRange中找到Sheet2中的值,那么对于相应的行,我想将A列中的值放入E列中

As it stands, I'm only able to look for a single value from Sheet2 ("A1"). 就目前而言,我只能从Sheet2(“ A1”)查找单个值。 When attempting to extend this range I get errors. 尝试扩展此范围时,出现错误。

Is there a way to make the range in Sheet2 dynamic, please? 请问有没有一种方法可以使Sheet2中的范围动态化?

Sub Find_values()

    Dim myRange As Range
    Dim Cell As Range
    Dim LR As Long

    LR = Sheets(1).Range("B" & Rows.Count).End(xlUp).Row
    Set myRange = Sheets(1).Range("B1:B" & LR)

    For Each Cell In myRange

    If Cell.Value = Sheets(2).Range("A1").Value Then Cell.Offset(0, 3) = Cell.Offset(0, -1).Value


    Next Cell

End Sub

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

Follow this steps: 请按照以下步骤操作:

Convert the data in Sheet1 to an structured Excel table: 将Sheet1中的数据转换为结构化的Excel表:

1- Select the range from scale's cell to the concatenate last cell 1-选择从刻度的单元格到串联的最后一个单元格的范围

2- Click on the Ribbon "Home" | 2-单击功能区“主页” | "Styles" | “样式” | "Format as table" | “格式化为表格” |

3- Check "My table has headers" box (mark it) 3-选中“我的表有标题”框(将其标记)

4- Write down the name of the table (While selecting one cell inside the table, look in the "Table tools" ribbon | "Table name" 4-写下表格的名称(在表格中选择一个单元格时,在“表格工具”功能区|“表格名称”中查找

5- Repeat the previous steps for the data in Sheet2 5-对Sheet2中的数据重复前面的步骤

6- Add the following code to a VBA module: 6-将以下代码添加到VBA模块:

Sub LookupValues()

    ' Define object variables
    Dim sourceSheet As Worksheet
    Dim sourceTable As ListObject
    Dim sourceCell As Range

    Dim dataSheet As Worksheet
    Dim dataTable As ListObject



    ' Define other variables
    Dim sourceSheetName As String
    Dim sourceTableName As String

    Dim dataSheetName As String
    Dim dataTableName As String


    ' >>>>Customize this<<<<<
    sourceSheetName = "Sheet2"
    sourceTableName = "Table2"
    dataSheetName = "Sheet1"
    dataTableName = "Table1"

    ' Initialize worksheets
    Set sourceSheet = ThisWorkbook.Worksheets(sourceSheetName)
    Set dataSheet = ThisWorkbook.Worksheets(dataSheetName)

    ' Initialize source table
    Set sourceTable = sourceSheet.ListObjects(sourceTableName)
    Set dataTable = dataSheet.ListObjects(dataTableName)

    ' Loop through every cell in sourceSheet
    For Each sourceCell In sourceTable.DataBodyRange.Columns(1).Cells
        ' >>>>Customize this<<<<<
        ' In the following code:
        ' Offset(0, 4) -> 4 stand for 4 columns after column A
        ' Index(dataTable.DataBodyRange.Columns(1) -> 1 stands to return the first column of the data table
        ' Match(sourceCell.Value, dataTable.DataBodyRange.Columns(2) -> 2 stands to look in the second column of the data table
        If Not IsError(Application.Match(sourceCell.Value, dataTable.DataBodyRange.Columns(2), 0)) Then
            sourceCell.Offset(0, 4).Value = Application.Index(dataTable.DataBodyRange.Columns(1), Application.Match(sourceCell.Value, dataTable.DataBodyRange.Columns(2), 0))
        End If

    Next sourceCell


End Sub

6- Customize the code to fit your needs 6-自定义代码以满足您的需求

7- Test it and let us know if it works Hope it helps! 7-测试它,让我们知道它是否有效,希望它能有所帮助!

As others suggested, you can simply nest two For Each loops. 正如其他人所建议的,您可以简单地嵌套两个For Each循环。

Sub Find_values()

    Dim myRange1 As Range
    Dim myRange2 As Range
    Dim Cell1 As Range
    Dim Cell2 as Range

    Set myRange1 = Sheets(1).Range(range("B1"),range("B1").end(xlDown))
    Set myRange2 = Sheets(2).Range(range("A1"),range("A1").end(xlDown)) 'This range is also dynamic and will adapt to teh number of entries you ahve in Sheet2

    For Each Cell1 In myRange1
    For Each Cell2 In myRange2

    If Cell1.Value2 = Cell2.Value2 Then
        Cell1.Offset(0, 3) = Cell1.Offset(0, -1).Value2
        Exit for 'Save you some useless processing time since the entry has already been found
    end If

    Next Cell2
    Next Cell1

End Sub

Note I have re-wrote your Range statment to be more cleaner. 请注意,我重新编写了Range语句,以使其更加清晰。 Note I have also changed your .value statement into .value2, which depending on the type of data used in your cell will in some cases work better. 注意,我也将您的.value语句更改为.value2,这在某些情况下取决于您单元格中使用的数据类型会更好。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM