• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Split, match, replace, Join, having trouble with the Join

Hello,

I am trying to split each cell from column A to the last column on ", " in sheet orgs

Then test each element against a master list in sheet A to see if any of the split elements have a match in the list

If so the replace the matched element of the split cell from Column B in sheet A

I can not get how to properly Re-Join my split cell with the replace values from the match and replacement

Ex:
sheet(Orgs)
Cells(2,1) = a,b,c,d

Sheet(A)
Code:
ColA  ColB
a        a
b        w
c        c
d        z

Then
Cells(2,1) = a,w,c,z

I appreciate any help on this I have been trying for hours, to no avail

I have uploaded a file

Thanks

Code:
Sub FindReplace_Orgs()
Dim FindValues As Variant, ReplaceValues As Variant, G As Variant, temp As Variant
Dim wsFV As Worksheet, wsRV As Worksheet
Dim sLR As Long, tLR As Long, i As Long, j As Long, n As Long

    'Sheets("Orgs").Select

    Set wsFV = ThisWorkbook.Worksheets("Orgs")
    Set wsRV = ThisWorkbook.Worksheets("A")

    sLR = wsFV.Range("A" & wsFV.Rows.Count).End(xlUp).Row
    tLR = wsRV.Range("A" & wsRV.Rows.Count).End(xlUp).Row

    For i = 2 To sLR
      For j = 2 To tLR
  
          G = Split(wsFV.Cells(i, 1).value, ", ")
          temp = G
          For n = LBound(G) To UBound(G)
            If splitCell(wsFV.Range("A" & i), ", ", n) = wsRV.Cells(j, "A") Then
              temp(i) = Join(wsRV.Cells(j, "B").value, ", ")
              'MsgBox G(n) & "  " & temp(i)
            End If
          Next n
    
      Next j
    Next i
    wsFV.Range("A2:A" & sLR).value = temp
End Sub

Helper Function

Code:
Function splitCell(strValue As String, delim As String, Optional n As Long) As String

  If strValue <> "" Then
    splitCell = Split(strValue, delim)(n)
  Else
    strValue = ""
  End If

End Function
 

Attachments

Last edited:
@Narayan

If I may ask you, is it possible to alter your code such that if my replacement data has empty cells it only update for cells with data

I have tried but I do not understand what is going on well enough to do it myself

Thank you

Ex
2018-02-23_21-18-14_zezvgw.jpg
 

Attachments

Hi ,

Can you take the following example :

Los Angeles Sherrif Dept, Whole Person Care, Housing for Health, Office of Immigrant Affairs

and post what the output will be ?

Narayan
 
Sorry for my lack of clarity

Here is what your example would become:
RRR_25, Whole Person Care, RRR_7, Office of Immigrant Affairs

If there is a replacement value then replace that for its corresponding value in List otherwise keep all the original values in the string as they are

Thanks again for your interest in my question
 
Back
Top