Link to home
Start Free TrialLog in
Avatar of Moxie co
Moxie co

asked on

Macro -- Duplicate Row if Semicolon is found in Cell

Hi,

I need to create a macro that looks in column K looking for any text after a semicolon ';' and creates a duplicate row containing the info.

Basically, the cell in the 'K' column looks like the following: test@gmail.com; moxie@gmail.com

I want to be able to break up and take moxie@gmail for example and include the entire row of info

Thanks!
Avatar of Member_2_25505
Member_2_25505

Use the Split function in VBA. If the count > 1 then you have 2 email addresses
Avatar of Norie
Would you split what's in column K so each email address would be in it's own row?

Would there ever be more than one email address in the cell?
Dim Tokens() As String

Tokens = Split(Sheets("SheetName").Range("K").value, ";")

If Tokens.Length > 1 Then
    'Put token 2 on its own row
End If

Open in new window

Avatar of Moxie co

ASKER

Hi Norie,

Each email would be in its own cell and on a seperate row, but would contain info from its previous row. Hope that makes sense!

 Trying to find the most effcient way of doing this
Sorry

Dim Tokens() As String

Tokens = Split(Sheets("SheetName").Range("K").value, ";")

If Tokens.Count> 1 Then
    'Put token 2 on its own row
End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for everyone's input!  Subodh your code worked perfectly!
You're welcome Moxie! Glad it worked. :)
If I were to include commas in addtion to semicolons. Would the code look like this:

  If InStr(Cells(i, "K"), ";") > 0 Or InStr(Cells(i, "K"), ",") > 0 Then
        str = Split(Cells(i, "K"), "; ")
        n = UBound(str)
        Rows(i).Copy
        Rows(i + 1).Resize(n).Insert Shift:=xlDown
        Cells(i, "K").Resize(n + 1).Value = Application.Transpose(str)
    End If


Sorry relatively new to programming!
Yeah the IF condition is okay but still the cell content is split based on ; delimiter. Why would you have both the commas and semicolon in the same cell as delimiters?
I noticed the cells are also separated by a comma as I was working on the spreadsheet.  Thanks for following up.
The chosen answer addressed the original question.