【问题标题】:VBA *Wildcard* Vlookup - or alternativeVBA *通配符* Vlookup - 或替代
【发布时间】:2015-01-13 06:23:51
【问题描述】:

我正在寻找使用 Vlookup 函数将值返回到相邻列的代码。但是我希望查找使用通配符,即不需要完全匹配。

下面的代码将逐行向下查找D列,然后使用该值从数据表中查找相应的值并将其返回到E列。这种工作但不能很好地处理缺失或不正确的值。

D列的数据将是全文格式的句子,所以我只需要寻找关键词,然后返回一个设定的参考值进行数据处理。

    Sub LookUpComments() 'exact match only ???

On Error Resume Next
Application.ScreenUpdating = False

    Dim DataRow As Long
    Dim DataClm As Long
    Dim Result As Variant

DataTable = Sheet3.Range("D5:D35")
LookUpTable = Sheet3.Range("AA10:AB20")
Sheet3.Range("E5:E10000").ClearContents

DataRow = Sheet3.Range("E5").Row
DataClm = Sheet3.Range("E5").Column

For Each cl In DataTable

        If cl = "" Then GoTo E
        Result = Application.WorksheetFunction.VLookup(cl, LookUpTable, 2, blnLookupType)
        If Result = Error Then GoTo E
        Sheet3.Cells(DataRow, DataClm) = Result

E:            DataRow = DataRow + 1
Next cl

Application.ScreenUpdating = True
MsgBox "Data LookUp is complete"

End Sub

我希望我已经说得够清楚了吗?如果这个功能不可用,你认为我可以使用某种循环查找和替换功能吗?

提前致谢

【问题讨论】:

    标签: excel vba wildcard vlookup


    【解决方案1】:

    如果使用 Vlookup 就像您在将 blnLookupType 设置为 true 时所做的那样没有给出您想要的结果,您可以使用 Range.Find,然后使用 Offset 返回您想要的值。它支持通配符,您可以搜索字符串的一部分或整个字符串。请参阅下面的示例。

    Sub FindingPart()
    
    Dim rng As Range, found As Range
    Set rng = Sheet2.Range("A:A")
    
    Set found = rng.Find(What:="as", LookAt:=xlPart) 'Will find for example "bass"
    If Not found Is Nothing Then returnValue = found.Offset(0, 1)
    
    End Sub
    
    
    
    Sub FindingWildCards()
    
    Dim rng As Range, found As Range
    Set rng = Sheet2.Range("A:A")
    
    Set found = rng.Find(What:="as*", LookAt:=xlWhole) 'Would find for example "ashes" but not "bass"
    If Not found Is Nothing Then returnValue = found.Offset(0, 1)
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      感谢您的建议,看来我只是使用 VLookup 走错了路。

      我现在编写了一个代码,它可以完美地满足我的需要,尽管我确信它可以写得更好。我使用 vlookup 来获取每个搜索条件的返回值,然后为每个搜索条件循环查找/替换。

      Sub FilterComments()
      
      On Error Resume Next
      
      Dim Rng As Range, found As Range
      Dim Rtn As String
      
      Application.ScreenUpdating = False
      Application.Calculation = xlCalculationManual
      
          LookUpValue = ActiveWorkbook.Sheets("LOOKUP").Range("I10:I108")
          LookUpTable = ActiveWorkbook.Sheets("LOOKUP").Range("I10:J108")
      
      ActiveWorkbook.Sheets("IEOutput").Range("W:W").Value = ActiveWorkbook.Sheets("IEOutput").Range("T:T").Value
      ActiveWorkbook.Sheets("IEOutput").Range("W1").Value = "LOOKUP COMMENT"
      
      Set Rng = ActiveWorkbook.Sheets("IEOutput").Range("W:W")
      
              For Each cl In LookUpValue
              If cl = "" Then GoTo E
      
                  Rtn = Application.VLookup(cl, LookUpTable, 2, False)
      
      
                  Rng.Replace What:="*" & cl & "*", Replacement:=Rtn, LookAt:=xlPart, _
                      SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
                      ReplaceFormat:=False
      E:
              Next cl
      
      
      Application.ScreenUpdating = True
      Application.Calculation = xlCalculationAutomatic
      MsgBox "Data LookUp is complete", vbInformation, "GM PMS Data Filter"
      
      End Sub
      

      希望这对尝试做类似事情的其他人有所帮助

      【讨论】:

        猜你喜欢
        • 2020-03-03
        • 1970-01-01
        • 2015-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-12
        • 1970-01-01
        • 2018-10-20
        相关资源
        最近更新 更多