Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel vba:在其他工作表中查找值_Excel_Search_Vba - Fatal编程技术网

Excel vba:在其他工作表中查找值

Excel vba:在其他工作表中查找值,excel,search,vba,Excel,Search,Vba,我试图从sheet2(A1:DF5000)中的sheet1中找到一个值。问题是该值可能位于表2中的任何位置。一旦找到匹配项,还有一件事;假设在sheet2 X495中,我需要它返回sheet2 X1的值 始终从搜索匹配返回列 始终返回第1行 始终使用精确匹配 所以,如果我在搜索“ABC”,并且在sheet2中找到了一个精确的匹配项!D14。它将返回值Sheet2!D1 真实世界应用程序: 我正在使用excel跟踪我用于产品的所有sku。每个站点都需要一个唯一的sku。所以我有数百个sku都是相同

我试图从
sheet2(A1:DF5000)
中的sheet1中找到一个值。问题是该值可能位于表2中的任何位置。一旦找到匹配项,还有一件事;假设在
sheet2 X495
中,我需要它返回
sheet2 X1的值

  • 始终从搜索匹配返回列
  • 始终返回第1行
  • 始终使用精确匹配
  • 所以,如果我在搜索“ABC”,并且在
    sheet2中找到了一个精确的匹配项!D14
    。它将返回值
    Sheet2!D1

    真实世界应用程序:

    我正在使用excel跟踪我用于产品的所有sku。每个站点都需要一个唯一的sku。所以我有数百个sku都是相同的产品。因此,我在excel中有一个主列表,第1行是我的产品,然后每列都有每个产品使用的所有sku

    下面的代码正在运行,但发生了一些有趣的事情。结果表明,它并不是在寻找精确的匹配,而是很接近

    有人能帮我把这个弄好吗

    如果我不清楚,请随时问我任何问题

    Function GetPart(text As Variant, rCells As Range)
      Dim txt As String
      Dim rRange As Range
      Dim SubjCell
    
      For Each rRange In rCells
        SubjCell = rRange
        txt = text
    
        If InStr(txt, SubjCell) <> 0 Then
          GetPart = SubjCell
          Exit For
        Else
          GetPart = "Not Found"
        End If
      Next rRange
    
    End Function
    
    函数GetPart(文本作为变量,rCells作为范围)
    以字符串形式显示文本
    模糊排列为范围
    暗室
    对于RCELL中的每个安排
    subjectcell=rRange
    txt=文本
    如果InStr(txt,subcell)为0,则
    GetPart=subjectcell
    退出
    其他的
    GetPart=“未找到”
    如果结束
    下一次安排
    端函数
    
    这就是您要尝试的吗?我已经对代码进行了注释,这样您在理解它时就不会有任何问题:)如果您这样做了,请告诉我

    Option Explicit
    
    Function GetPart(rng As Range, rngDB As Range) As Variant
        Dim aCell As Range, strSearch as String
    
        GetPart = 0
    
        On Error GoTo Whoa:
    
        '~~> This is the value which we will search
        strSearch = rng.Value
    
        '~~> Notice the use of LookAt:=xlWhole. This is to ensure that we
        '~~> do not get False Positives. Using INSTR will give you False
        '~~> Positives. For example searching for "Coke" in "DigitalCoke"
        Set aCell = rngDB.Find(What:=strSearch, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
    
        '~~> If the match is found
        If Not aCell Is Nothing Then
            '~~> Get value from the first row
            GetPart = aCell.Offset(-(aCell.Row - 1))
        End If
    
        Exit Function
    
    Whoa:
    End Function
    

    参见第4节Hanks Siddharth第5节运行良好,您知道如何返回结果列第1行的值吗?
    单元格(1,aCell.column).value
    ?Where
    aCell
    是您找到匹配项的单元格……几周来,我一直在尝试获取“Cells(1,aCell.Column).Value?Where-aCell”来工作。我真的很感激你的回答,我想自己解决这个问题。但我就是无法让它发挥作用。我应该把“单元格(1,aCell.Column).Value”放在哪里?aCell在哪里?谢谢。哦,这也很重要。它用于搜索的我的sku在数百个sku的主控表中是10AAAMBMFCFf92304924,一个是0AAAMB,另一个是10AAAMB。此搜索在应返回10AAAMB时返回0AAAMB。“10AAAMBFCFF92304924”在F485中,要返回的sku在F1中。0AAAMB在E1中(这是它返回的单元格)。如果我将0AAAMB移动到更远的列,它将返回正确的sku。但是如果说对sku的搜索是B99-3234-2349-a,那么它将根本找不到sku。我的搜索类型中一定有错误。