Instead of typing up a bunch of “Or” statements, how can I implement a function in this code?

前端 未结 3 535
攒了一身酷
攒了一身酷 2021-01-20 17:55
Sub test()

Dim DataRange As Range
Dim LastRow As Integer
Dim i As Integer
Dim SplitVal() As String
Dim OutputOffset As Long
OutputOffset = 0

LastRow = Cells(Rows.C         


        
3条回答
  •  生来不讨喜
    2021-01-20 18:04

    Your 50 accounts are probably in a list which is available in your worksheet. You can create a strong of those accounts and use the instr function to find if there is a match.

        Sub test()
    
            Dim DataRange As Range
            Dim LastRow As Integer
            Dim i As Long
            Dim SplitVal() As String
            Dim OutputOffset As Long
            OutputOffset = 0
    
            Dim Spike As String
            For i = 3 To 11
                Spike = Spike & Cells(i, 1).Value & "|"
            Next i
    
            LastRow = Cells(Rows.Count, "J").End(xlUp).Row
    
            For i = 2 To LastRow
                If InStr(Spike, Cells(i, 10).Value) Then
        '        If InStr(1, Cells(i, 10).Value, "Test1", vbTextCompare) <> 0 Or
        '           InStr(1, Cells(i, 10).Value, "Test2", vbTextCompare) <> 0 Or
        '           InStr(1, Cells(i, 10).Value, "Test3", vbTextCompare) <> 0 Then
    
                  SplitVal = Split(Cells(i - 2, 10).Value, " ", 2)
                  Cells(i + OutputOffset, 13).Value = SplitVal(0)
                  Cells(i + OutputOffset, 14).Value = SplitVal(1)
    
                  Cells(i + OutputOffset, 15).Value = Cells(i + 1, 10).Value
                End If
            Next i
    
    End Sub
    

    In my example the list is in A3:A11 on the ActiveSheet. If that doesn't work for you, place the list on another sheet and change the above code as follows.

    Dim WsList As Worksheet
    Dim Spike As String
    Set WsList = Worksheets("AccountList")
    For i = 3 To 11
        Spike = Spike & WsList.Cells(i, 1).Value & "|"
    Next i
    

提交回复
热议问题