简体   繁体   English

基于条件的Excel查找

[英]Excel lookup based on a condition

sheet1 sheet2  sheet3

 ---------
 |       |
 V       V      *  V-----
123  | A 123 | 456 C |  |
 *   | B 123 | 789 D |  |
     | C 123 | 345 E |  |
       ^                |
       |-----------------     

Can I look up 123 from sheet 1 to sheet 2 to return a letter (but that letter must appear in sheet 3 (C), look up the letter that is in sheet 3 and return 456? the problem is there are multiple 123's in sheet 2; I'm only used to dealing with unique numbers. Can it go A is not in sheet 3 so go to next letter until hits C. then lookup value to the left which is 456. 我可以从工作表1到工作表2查找123以返回字母(但是该字母必须出现在工作表3(C)中,查找工作表3中的字母并返回456吗?问题是工作表中有多个123 2;我只习惯于处理唯一数字,A不在表3中,所以转到下一个字母,直到命中C,然后向左查找值456。
Thanks 谢谢

Lets this is your data: Sheet1: 让这是您的数据:Sheet1:

在此处输入图片说明

Sheet2 : Sheet2:

在此处输入图片说明

Sheet 3: 工作表3:

在此处输入图片说明

The code below will loop through the values in sheet2 if a match is found it will loop through the values in sheet3. 如果找到匹配项,下面的代码将循环遍历sheet2中的值,它将遍历sheet3中的值。 If a match is found it will be displayed, else it will c continue its loop in sheet. 如果找到匹配项,则将显示它,否则它将继续在工作表中循环。

Sub main()
Dim intValue As Integer
Dim i As Integer
Dim j As Integer

Dim strChar As String

intValue = Sheet1.Cells(1, 1)
For i = 1 To 3
    If intValue = Sheet2.Cells(i, 2) Then
        strChar = Sheet2.Cells(i, 1)
        For j = 1 To 3
            If strChar = Sheet3.Cells(j, 2) Then
                MsgBox (Sheet3.Cells(j, 1))
                Exit Sub
            End If

        Next j

    End If

Next i
End Sub

Using VBA, inside a Module, write this new function: 使用VBA,在模块内部,编写以下新功能:

Public Function LookFx(Sh1 As Range, Sh2 As Range, Sh3 As Range) As String
Dim BaseVal As String
Dim FoundV As Boolean
Dim SecVal As String

Application.Volatile

BaseVal = Sh1.Value
FoundV = False

For Each xx In Sh2
    If xx.Value = BaseVal Then
        SecVal = xx.Offset(0, -1).Value
        For Each yy In Sh3
            If yy.Value = SecVal Then
                LookFx = yy.Offset(0, -1).Value
            End If
        Next
    End If
Next
End Function

the value to be add in the function are: 要添加到函数中的值是:

在此处输入图片说明

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

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