简体   繁体   English

VBA 以逗号分隔返回多个查找值的代码有效,但如果有空单元格则崩溃

[英]VBA code to return multiple lookup values in one comma separated works but crashes if there's an empty cell

I've found this code online which works like vlookup function but returns multiple data in one cell separated by comma and it works most of the time.我在网上找到了这段代码,它的工作方式类似于 vlookup function,但在一个单元格中返回多个数据,这些数据以逗号分隔,并且大部分时间都有效。 But when there's a blank cell in the lookup value it causes a crash.但是当查找值中有一个空白单元格时,它会导致崩溃。 It takes a long time to process too.处理的时间也很长。 I tried to tinker with it but I'm completely new to VBA coding.我试着修改它,但我对 VBA 编码完全陌生。 I was wondering if anyone could please help me fix the issue and maybe optimize the code a little so it doesn't crash or take as long.我想知道是否有人可以帮我解决这个问题,也许可以稍微优化一下代码,这样它就不会崩溃或花费很长时间。 Lookup value Table array查找值表数组

Here's the code这是代码

    Function MultiVLookUp(LookupValue As String, LookupRange As Range, ColumnNumber As Integer, Char As String)
    Dim I As Long
    Dim xRet As String
    For I = 1 To LookupRange.Columns(1).Cells.Count
        If LookupRange.Cells(I, 1) = LookupValue Then
            If xRet = "" Then
                xRet = LookupRange.Cells(I, ColumnNumber) & Char
            Else
                xRet = xRet & "" & LookupRange.Cells(I, ColumnNumber) & Char
            End If
        End If
    Next
    MultiVLookUp = Left(xRet, Len(xRet) - 1)
End Function

Thanks in advance.提前致谢。

Multi VLookUp: Delimited Return (UDF)多 VLookUp:定界返回 (UDF)

  • You'll use it in the same way as before, only I set the last parameter, the parameter of the Char (Delimiter) argument, as optional (default) to your 'favorite' ", " so you don't need to add it anymore.您将以与以前相同的方式使用它,只是我将最后一个参数,即Char (定界符)参数的参数设置为可选(默认)到您的“收藏夹” ", "因此您不需要添加它了。
  • If you were using an array formula, don't do it anymore.如果您使用的是数组公式,请不要再这样做了。
Option Explicit

Function MultiVLookUp( _
    ByVal LookupValue As String, _
    ByVal LookupRange As Range, _
    ByVal ColumnNumber As Long, _
    Optional ByVal Char As String = ", ") _
As String
    
    If Len(LookupValue) = 0 Then Exit Function
    
    Dim lData As Variant
    Dim vData As Variant
    Dim lrCount As Long
    
    With LookupRange
        lrCount = .Rows.Count
        If lrCount = 1 Then
            ReDim lData(1 To 1, 1 To 1): lData(1, 1) = .Columns(1).Value
            ReDim vData(1 To 1, 1 To 1): vData(1, 1) _
                = .Columns(ColumnNumber).Value
        Else
            lData = .Columns(1).Value
            vData = .Columns(ColumnNumber).Value
        End If
    End With
           
    Dim r As Long
    Dim rString As String
    
    For r = 1 To lrCount
        If CStr(lData(r, 1)) = LookupValue Then
            rString = rString & CStr(vData(r, 1)) & Char
        End If
    Next r
    If Len(rString) = 0 Then Exit Function
    
    MultiVLookUp = Left(rString, Len(rString) - Len(Char))

End Function

Im not really sure the point of this macro considering it just outputs the same value repeatedly but here ya go.我不太确定这个宏的意义,因为它只是重复输出相同的值,但这里是 go。

Sub main()

    ' ws is the worksheet object referencing "Sheet1"
    Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
                                
                                      ' Top Left (r, c)          (r, c) Bottom Right
    Dim rng As Range: Set rng = ws.Range(ws.Cells(1, 1), ws.Cells(6, 6))

    Dim lookupValue As String: lookupValue = ""

    Dim outStr As String: outStr = rangeValuesToString(rng, lookupValue)

    Debug.Print outStr

End Sub

Function rangeValuesToString(rng As Range, lookupValue As String) As String

    Dim topRow As Integer: topRow = rng.Row
    Dim botRow As Integer: botRow = rng.Row - 1 + rng.Rows.Count

    Dim leftCol As Integer: leftCol = rng.Column
    Dim rightCol As Integer: rightCol = rng.Column - 1 + rng.Columns.Count

    Dim i  As Integer, j As Integer
    Dim outStr As String: outStr = ""

    ' Iterates through each column moving left to right
    For i = leftCol To rightCol
        For j = topRow To botRow
            If rng.Cells(j, i).Value = lookupValue Then
                outStr = outStr & rng.Cells(j, i).Value & ", "
            End If
        Next j
    Next i

    rangeValuesToString = Left(outStr, Len(outStr) - 2)

End Function

暂无
暂无

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

相关问题 在单元格中查找以逗号分隔的多个值并返回以逗号分隔的多个值 - Lookup multiple values separated by a comma in a cell and return multiple values separated by a comma 动态查找单元格中的多个值(逗号分隔)并将相应的 ID 返回到单个单元格(逗号分隔) - Dynamic Lookup for multiple values in a cell (comma separated) and return the corresponding ID to a single cell (comma separated also) 在单个单元格中查找多个值(以逗号分隔),然后将值返回到单个单元格(也以逗号分隔) - Lookup multiple values in a single cell (separated by commas) and then return the values to a single cell (also comma separated) VBA 代码扫描单元格中的逗号分隔值并检索查找值 - VBA code to scan through a comma separated value in a cell and retrieve lookup value 如何从逗号分隔的单元格中查找多个值并在excel中平均结果? - How do I lookup multiple values from a comma separated cell and average the results in excel? 使用 VBA 在一个单元格中查找每个逗号分隔值并返回电子邮件 - Using VBA to vlookup each comma separated value in one cell and return emails Excel2011-在日期之前查找并将多个值返回到一个单元格 - Excel2011 - Lookup and return multiple values into one cell BY DATE 查找 MULTIPLE 值并在满足至少一个条件时返回特定单元格 - Lookup for MULTIPLE values and return a specific cell if AT LEAST ONE criteria is met "Excel:在一个单元格中查找多个值并在另一个单元格中返回结果" - Excel: Lookup multiple values in one cell and return results in another 如何使用 vba 代码在一个单元格中获取多个查找值,用于静态 LookupValue、LookupRange、ColumnNumber、来自不同工作表的分隔符 - how to get multiple lookup values in one cell using vba code for static LookupValue, LookupRange , ColumnNumber, delimiter from different sheets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM