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

时间:2019-02-02 11:44:02

标签: excel vba

对于“ myRange”中的每个单元格,我要检查Sheet2中的值范围,如果在myRange中找到Sheet2中的值,那么对于相应的行,我想将A列中的值放入E列中

就目前而言,我只能从Sheet2(“ A1”)查找单个值。尝试扩大此范围时,我会收到错误消息。

请问有没有一种方法可以使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

enter image description here

enter image description here

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

请按照以下步骤操作:

转换的数据在Sheet到结构化的Excel表格:

1-选择从标尺的单元格到串联的最后一个单元格的范围

2-单击功能区“主页” | “样式” | “格式如表” |

3-检查 “我的表具有标题” 框(其标记)

4-记下表的名称(在选择的表内的一个单元格,看在“表工具”色带|“表名”

5-对Sheet2中的数据重复上述步骤

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-定制代码根据自己的需要

7-测试它,让我们知道它是否有效 希望对您有帮助!

答案 1 :(得分:0)

正如其他人所建议的,您可以简单地嵌套两个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

请注意,我已经重新编写了Range语句,以使其更加清晰。 请注意,我还将您的.value语句更改为.value2,这在某些情况下取决于您单元格中使用的数据类型会更好。