VBA To create unique value from Cell with 2 words - Split Value - Excel 2010

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. MacOS
Hello,

I am working on a VBA project to break-out a cell that has two names listed into unique individual lines. I will attach a screenshot to elaborate on this.


Excel 2012
ABCDE
1Unique ValueStateColumn AColumn BColumn C
2ABC3CaliforniaTest1HappyLeft
3ABC4TexasTest1, Test2, Test3SadRight
4ABC5OhioTest2TiredRight
5ABC6North CarolinaTest3, Test4HappyRight
6ABC7GeorgiaTest1TiredLeft
7ABC8WashingtonTest1SadStraight
Sheet1


What I am trying to accomplish is to have a new row entry for each cell in Column A that has a "," followed by a new word.

So using the attached data for example Column A, Row 3 should result in 2 additional rows with all data duplicated and only include 1 of the 3 names from Cell C3 and remove the ",".

Hopefully I am explaining this well.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Johnny Thunder,

If I understand you correctly.

Sample raw data:


Excel 2007
ABCDE
1Unique ValueStateColumn AColumn BColumn C
2ABC3CaliforniaTest1HappyLeft
3ABC4TexasTest1, Test2, Test3SadRight
4ABC5OhioTest2TiredRight
5ABC6North CarolinaTest3, Test4HappyRight
6ABC7GeorgiaTest1TiredLeft
7ABC8WashingtonTest1SadStraight
8
9
10
11
Sheet1


After the macro:


Excel 2007
ABCDE
1Unique ValueStateColumn AColumn BColumn C
2ABC3CaliforniaTest1HappyLeft
3ABC4TexasTest1SadRight
4ABC4TexasTest2SadRight
5ABC4TexasTest3SadRight
6ABC5OhioTest2TiredRight
7ABC6North CarolinaTest3HappyRight
8ABC6North CarolinaTest4HappyRight
9ABC7GeorgiaTest1TiredLeft
10ABC8WashingtonTest1SadStraight
11
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub ReorgData()
' hiker95, 07/22/2014, ME793392
Dim r As Long, lr As Long, s
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 1).End(xlUp).Row
For r = lr To 2 Step -1
  If InStr(Cells(r, 3), ", ") > 0 Then
    s = Split(Cells(r, 3), ", ")
    Rows(r + 1).Resize(UBound(s)).Insert
    Range("A" & r & ":E" & r).Resize(UBound(s) + 1).Value = Range("A" & r & ":E" & r).Value
    Cells(r, 3).Resize(UBound(s) + 1).Value = Application.Transpose(s)
  End If
Next r
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the ReorgData macro.
 
Upvote 0
Johnny Thunder,

If my resulting screenshot is not correct, then please attach a screenshot of the results (manually formatted by you) that you are looking for.
 
Upvote 0
Hiker95,

You Sir, are amazing! It worked flawlessly and your interpretation of what I posted was spot on!

I only have one question so I can learn from your code.

So the data I posted was just Test data, my actual report starts in column A and ends in column H with column H being the column that has the multiple values.

Would I simply change this piece of code

"Range("A" & r & ":E" & r)" to "Range("A" & r & ":H" & r) ?
 
Upvote 0
Johnny Thunder

Thanks for the feedback.

You are very welcome. Glad I could help.

So the data I posted was just Test data

It is always best to display your actual raw data worksheet(s), and, the results that you are looking for. This way we can usually find a solution on the first go.

Would I simply change this piece of code

"Range("A" & r & ":E" & r)" to "Range("A" & r & ":H" & r) ?

Without seeing your actual raw data, it is difficult to say.

I would suggest that you try the change your have provided.

If the change does not work, then post a screenshot of your actual raw data, and, a screenshot (manually formatted by you) of the results you are looking for.
 
Upvote 0
I was able to get what I wanted with the proposed changes.

thank you again!
 
Upvote 0
Johnny Thunder,

Thanks for the feedback.

You are very welcome, again. Glad your changes worked correctly.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,215,655
Messages
6,126,054
Members
449,283
Latest member
GeisonGDC

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top