New row for comma-separated values

Gain

New Member
Joined
Jun 4, 2012
Messages
45
Hi,

A sample spreadsheet of what I have is provided below:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]ID NUMBER[/TD]
[TD]CHEMICAL NAME[/TD]
[TD]HAZARD[/TD]
[TD]VALUE[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]chemical a[/TD]
[TD]carcinogen[/TD]
[TD]recognized[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]chemical a[/TD]
[TD]Respiratory, blood, kidney[/TD]
[TD]suspected[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]chemical b[/TD]
[TD]immunotoxic, carcinogen[/TD]
[TD]suspected[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]chemical c[/TD]
[TD]liver, carcinogen[/TD]
[TD]recognized[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]chemical d[/TD]
[TD]kidney[/TD]
[TD]recognized[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]chemical d[/TD]
[TD]neurotoxic[/TD]
[TD]suspected[/TD]
[/TR]
</tbody>[/TABLE]

Only the words 'recognized' or 'suspected' can be entered into the VALUE column and hazards that are either recognized or suspected of being caused by that chemical in that row are listed in the HAZARD column. I need to have a separate entry for each hazard, like the following table, using the same chemicals as above:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]CAS NUMBER[/TD]
[TD]CHEMICAL NAME[/TD]
[TD]HAZARD[/TD]
[TD]VALUE[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]chemical a[/TD]
[TD]carcinogen[/TD]
[TD]recognized[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]chemical a[/TD]
[TD]respiratory[/TD]
[TD]suspected[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]chemical a[/TD]
[TD]blood[/TD]
[TD]suspected[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]chemical a[/TD]
[TD]kidney[/TD]
[TD]suspected[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]chemical b[/TD]
[TD]immunotoxic[/TD]
[TD]suspected[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]chemical b[/TD]
[TD]carcinogen[/TD]
[TD]suspected[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]chemical c[/TD]
[TD]liver[/TD]
[TD]recognized[/TD]
[/TR]
[TR]
[TD]3 [/TD]
[TD]chemical c[/TD]
[TD]carcinogen[/TD]
[TD]recognized[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]chemical d [/TD]
[TD]kidney[/TD]
[TD]recognized[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]chemical d[/TD]
[TD]neurotoxic[/TD]
[TD]suspected[/TD]
[/TR]
</tbody>[/TABLE]

How can I create a new row for each comma-separated hazard?

Thank you in advance :)
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Gain,


Sample raw data:


Excel Workbook
ABCD
1ID NUMBERCHEMICAL NAMEHAZARDVALUE
21chemical acarcinogenrecognized
31chemical aRespiratory, blood, kidneysuspected
42chemical bimmunotoxic, carcinogensuspected
53chemical cliver, carcinogenrecognized
64chemical dkidneyrecognized
74chemical dneurotoxicsuspected
8
9
10
11
12
Sheet1





After the macro:


Excel Workbook
ABCD
1ID NUMBERCHEMICAL NAMEHAZARDVALUE
21chemical acarcinogenrecognized
31chemical aRespiratorysuspected
41chemical abloodsuspected
51chemical akidneysuspected
62chemical bimmunotoxicsuspected
72chemical bcarcinogensuspected
83chemical cliverrecognized
93chemical ccarcinogenrecognized
104chemical dkidneyrecognized
114chemical dneurotoxicsuspected
12
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, by highlighting the code and pressing the keys CTRL + C
2. Open your 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 by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub ReorgData()
' hiker95, 06/20/2012
' http://www.mrexcel.com/forum/showthread.php?641962-New-row-for-comma-separated-values
Dim lr As Long, r As Long, Sp
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
    Sp = Split(Cells(r, 3), ", ")
    Rows(r + 1).Resize(UBound(Sp)).Insert
    Rows(r + 1).Resize(UBound(Sp)).Value = Rows(r).Value
    Cells(r, 3).Resize(UBound(Sp) + 1).Value = Application.Transpose(Sp)
  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

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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