Split array and re join

mdo8105

Board Regular
Joined
Nov 13, 2015
Messages
83
I have an array that I am able to split and get the individual text in it, then I take each string and find match on another sheet and grab the Id in the next column. I want to take the ID that I find and re join into an array.
Code:
v1 = Split(ws1.Cells(r, 7), ",")For i = 0 To UBound(v1)
           v2 = Application.WorksheetFunction.Index(ws2.Range("D:D"), Application.WorksheetFunction.Match(v1(i), ws2.Range("C:C"), 0))
           
          
        
        Next i
I want to take all that is returned in v2 and make an array delimited by commas and return back into a cell. I've Join, but I get a type mismatch with v2 variable.
 

Eric W

MrExcel MVP
Joined
Aug 18, 2015
Messages
8,801
You can just concatenate the results as you go, like:
Rich (BB code):
Dim str1 as String
v1 = Split(ws1.Cells(r, 7), ",")
For i = 0 To UBound(v1)
    v2 = Application.WorksheetFunction.Index(ws2.Range("D:D"), Application.WorksheetFunction.Match(v1(i), ws2.Range("C:C"), 0))
    str1 = str1 & "," & v2
Next i
Cells(r, 8) = Mid(str1, 2)
If you want to use Join, something like:
Rich (BB code):
Dim Str1() as string

v1 = Split(ws1.Cells(r, 7), ",")
Redim Str1(0 to UBound(v1)) 
For i = 0 To UBound(v1)
     Str1(i) = Application.WorksheetFunction.Index(ws2.Range("D:D"), Application.WorksheetFunction.Match(v1(i), ws2.Range("C:C"), 0))
Next i
Cells(r, 8) = Join(Str1,",")
 
Last edited:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,425
Office Version
365
Platform
Windows
How about something like
Code:
   Dim v1 As Variant, v2 As Variant
   Dim v3 As String
   Dim i As Long
   
   v1 = Split(Ws1.Cells(r, 7), ",")
   For i = 0 To UBound(v1)
      v2 = Application.Index(Ws2.Range("D:D"), Application.Match(v1(i), Ws2.Range("C:C"), 0))
      If Not IsError(v2) Then
         If v3 = "" Then v3 = v2 Else v3 = v3 & "," & v2
      End If
   Next i
   Ws1.Cells(r, 8).Value = v3
   v3 = ""
 

mdo8105

Board Regular
Joined
Nov 13, 2015
Messages
83
So sorry for the late response. Eric W, that code worked like a charm. Thank you so much.
 

Forum statistics

Threads
1,078,226
Messages
5,338,958
Members
399,272
Latest member
jakepenner

Some videos you may like

This Week's Hot Topics

Top