excel 在分隔为“,”的列中搜索值

3phpmpom  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(95)

我在一个用户表单中使用了这样的代码,即我在txtreg(textBox)中搜索一个值,该值位于A或D列中。并将结果显示在名为(Txtledglist)的列表框中。结果在B列中,当在txtledglist(ListBox)中显示时,它与C列中同一行中的相应值连接。
现在A中的值是正常的(1值),但在D中,我正在搜索的一些值将被“,”分隔。
因此,在我下面的代码中,当A或D中只有1个值时,它也会完美地搜索,但是当值之间有“,”时,它不会找到任何东西。我怎么能补充说,它也搜索列D中每个“,”之间的值?
我希望我已经解释了我的想法…谢谢大家
代码如下:

Private Sub txtreg_Change()

    Dim lookupValue As String: lookupValue = txtreg.value
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim ws As Worksheet: Set ws = wb.Sheets("L 403")
    
    txtledglist.Clear
    
    Dim rng As Range: Set rng = ws.Range("A:A,D:D")  ' Update the range to include columns A and D
    On Error Resume Next
    Dim firstCell As Range: Set firstCell = rng.Find(What:=lookupValue, LookIn:=xlValues, LookAt:=xlWhole)
    On Error GoTo 0
    
    If Not firstCell Is Nothing Then
        Dim cell As Range: Set cell = firstCell
        Dim results() As Variant
        Dim count As Long: count = 0
        
        Do
            count = count + 1
            Set cell = rng.FindNext(cell)
        Loop While Not cell Is Nothing And cell.Address <> firstCell.Address
        
        ReDim results(1 To count, 1 To 1)
        
        Set cell = rng.Find(What:=lookupValue, LookIn:=xlValues, LookAt:=xlWhole)
        
        Dim index As Long: index = 1
        Do
            results(index, 1) = ws.Cells(cell.Row, "B").value & "  " & ws.Cells(cell.Row, "C").value
            index = index + 1
            Set cell = rng.FindNext(cell)
        Loop While Not cell Is Nothing And cell.Address <> firstCell.Address
        
        txtledglist.List = results
    End If
End Sub
2nc8po8w

2nc8po8w1#

您可以拆分列D中的值,并检查是否有任何单个值匹配。

Private Sub txtreg_Change()

    Dim lookupValue As String: lookupValue = txtreg.value
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim ws As Worksheet: Set ws = wb.Sheets("L 403")
    
    txtledglist.Clear
    
    Dim rng As Range: Set rng = ws.Range("A:A,D:D")  ' Update the range to include columns A and D
    On Error Resume Next
    Dim firstCell As Range: Set firstCell = rng.Find(What:=lookupValue, LookIn:=xlValues, LookAt:=xlWhole)
    On Error GoTo 0
    
    If Not firstCell Is Nothing Then
        Dim cell As Range: Set cell = firstCell
        Dim results() As Variant
        Dim count As Long: count = 0
        
        Do
            count = count + 1
            Set cell = rng.FindNext(cell)
        Loop While Not cell Is Nothing And cell.Address <> firstCell.Address
        
        ReDim results(1 To count, 1 To 1)
        
        Set cell = rng.Find(What:=lookupValue, LookIn:=xlValues, LookAt:=xlWhole)
        
        Dim index As Long: index = 1
        Do
            Dim values() As String
            values = Split(ws.Cells(cell.Row, "D").Value, ",") ' Split the values in column D by comma
            
            ' Iterate through each split value and check if it matches the lookupValue
            For Each value In values
                If Trim(value) = lookupValue Then
                    results(index, 1) = ws.Cells(cell.Row, "B").value & "  " & ws.Cells(cell.Row, "C").value
                    index = index + 1
                    Exit For ' Exit the loop if a match is found
                End If
            Next value
            
            Set cell = rng.FindNext(cell)
        Loop While Not cell Is Nothing And cell.Address <> firstCell.Address

        txtledglist.List = results

    End If
End Sub

相关问题