Excel 从更多 sheet 中搜索并复制

Excel search and copy from more sheet

我有一个 file.xls 和三个 sheet。

Sheet1,2 列,3000 行;

Sheet2,2 列,5000 行;

Sheet3,2 列,6000 行;

如何使用以下语法将数据分组到新的 sheet4(查看图片);

location_id 在 sheet 1 中获取 location_label 名称,location_id 在 sheet2 中获取 screen_id 值,screen_id在 sheet3 和 sheet4 中获取 screen_name 值 location_label 和 screen_name.

#使用 VLOOKUP 编辑问题;

我尝试使用 VLOOKUP 但在 sheet2 的第一个 ID 号 19 之后出现错误...我使用了这个 '

=VLOOKUP(Sheet2!A2;Sheet1!A2:B2133;2;)

我得到

好吧,我不是最擅长 Excel 的,而且我不完全理解您想要的最终结果。但这是我的猜测,我认为是问题所在。

我怀疑在 sheet 4 中,您在 A2 中键入代码并使用鼠标将公式拖到列的底部。如果是这样的话,你的公式不会在每个单元格中都完全一样,因为你在公式中的矩阵会随着你拖动鼠标而改变。因此错误#N/D

要防止您的矩阵在拖动鼠标时发生变化,您应该使用“$”。

所以你的公式是:

=VLOOKUP(Sheet2!A2;Sheet1!$A:$B33;2;)

可能是一个提示: 我看到你有超过 1 sheet 个相同的列。您可以将所有数据放在 1 sheet 中,然后使用过滤器 select 您想要的数据。请参阅 this 文章。

Public Sub pair_value()

'here i tried to deconstruct the code so it is easy to follow
'this type of paring would work much better with access
'you can use this code to start
Dim h1 As Integer 'row count in sheet1
Dim h2 As Integer 'row count in sheet2
Dim h3 As Integer 'row count in sheet3
Dim h4 As Integer 'row count in sheet4
Dim ar1() As Variant
Dim ar2() As Variant
Dim ar3() As Variant
Dim ar4() As Variant
Dim pair1() As Variant

Dim range1 As Range
Dim range2 As Range
Dim range3 As Range

Dim i As Integer
Dim j As Integer

'---------------------------------------------------
'This exercise would be so much easier using ACCESS
'---------------------------------------------------

'number of rows in each sheets
h1 = Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Row
h2 = Worksheets(2).Cells(Rows.Count, 1).End(xlUp).Row
h3 = Worksheets(3).Cells(Rows.Count, 1).End(xlUp).Row
h4 = Worksheets(4).Cells(Rows.Count, 1).End(xlUp).Row

'define arrays
ReDim ar1(h1, 2)
ReDim ar2(h2, 2)
ReDim ar3(h3, 2)


ReDim pair1(h2, 2)

'set range
Set range1 = Worksheets(1).Range(Worksheets(1).Cells(2, 1), Worksheets(1).Cells(h1, 2))
Set range2 = Worksheets(2).Range(Worksheets(2).Cells(2, 1), Worksheets(2).Cells(h2, 2))
Set range3 = Worksheets(3).Range(Worksheets(3).Cells(1, 1), Worksheets(3).Cells(h3, 2))

'load range into arrays
ar1 = range1
ar2 = range2
ar3 = range3

'associate location_label to screen_id using location_id as primary key
For i = 1 To UBound(ar2)
   
    For j = 1 To UBound(ar1)
        If ar2(i, 1) = ar1(j, 1) Then
            'load screen id + label in pair1 in pair1 array
            pair1(i, 1) = ar2(i, 2)
            pair1(i, 2) = ar1(j, 2)
            
        End If
    Next j
Next i

 
'associate location_label to screen_name using screen_id as primary key
For i = 1 To UBound(ar3)
    
    For j = 1 To UBound(pair1)
        If ar3(i, 1) = pair1(j, 1) Then
            Debug.Print j
            'past results in sheets(4)
             h4 = Worksheets(4).Cells(Rows.Count, 1).End(xlUp).Row
             Worksheets(4).Cells(h4 + 1, 1).Value = pair1(j, 2)
             Worksheets(4).Cells(h4 + 1, 2).Value = ar3(i, 2)
        End If
    Next j
Next i

结束子