Splitting data in cells w/VBA macro

seattletimebandit

Board Regular
Joined
Apr 11, 2013
Messages
69
Curious to know if there is a way to take data from one table and reorder the data into into another table. My project is creating tables of soil sample data collected for environmental cleanup.


First table is output from another VBA routine.


  • NOTE: the "GP-1" is the location of a soil sample collected; the trailing numbers are the depth the sample was collected ("1.8" feet, "5.5" feet, etc.)

GP-1-1.8GP-1-5.5GP-1-18.25
GP-1-21
GP-1-26GP-1-34GP-1-40
Benzene0.00844 U0.0074 U0.00749 U0.0128 U0.00885 U0.00889 U0.00901 U
Toluene0.0242 U0.0212 U0.0214 U0.0205 U0.0253 U0.0254 U0.0258 U
Ethylbenzene0.0302 U0.0265 U0.0268 U0.0256 U0.0317 U0.0318 U0.0322 U
mp-Xylene0.0604 U0.0529 U0.0536 U0.0513 U0.0633 U0.0636U0.0644 U
o-Xylene0.0302 U0.0265 U0.0268 U0.0256 U0.0317 U0.0318 U0.0322 U
Gasoline61.045001000101515
PCE6.045.295.365.136.336.366.44

<tbody>
</tbody>

I need to split the depths from the Sample ID. So it's copying the Sample ID and pasting to the first row/col, splitting off the depths into the following columns, or deleting the Sample ID from those columns, leaving only the depth (also removing the second dash "-")).

The output table looks like this:

GP-1
1.8
5.518.2521263440
Benzene0.00844 U0.0074 U0.00749 U0.0128 J0.00885 U0.00889 U0.00901 U
Toluene0.0242 U0.0212 U0.0214 U0.0205 U0.0253 U0.0254 U0.0258 U
Ethylbenzene0.0302 U0.0265 U0.0268 U0.0256 U0.0317 U0.0318 U0.0322 U
mp-Xylene0.0604 U0.0529 U0.0536 U0.0513 U0.0633 U0.0636 U0.0644 U
o-Xylene0.0302 U0.0265 U0.0268 U0.0256 U0.0317 U0.0318 U0.0322 U
Gasoline61.045001000101515
Diesel0.0604 U0.0529 U0.0536 U0.0513 U0.0633 U0.0636 U0.0644 U
PCE6.045.295.365.136.336.366.44

<tbody>
</tbody>


















Is this even possible?

Thanks in advance!

stb
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
You question is not state with enough specifics....

Where is the table? I'll assume it start in cell A1.

Do you want to fix the existing table in place or create a new table? I'll assume fix it in place.

Here is code will do what you want if my assumptions are correct...
Code:
[table="width: 500"]
[tr]
	[td]Sub SplitDepthsFromSampleID()
  Dim ID As String
  ID = Left(Range("B1").Value, InStrRev(Range("B1").Value, "-") - 1)
  Range("A1").Value = ID
  Range("B1", Cells(1, Columns.Count).End(xlToLeft)).Replace ID & "-", "", xlPart, , , , False
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Rick,


You assumed correctly, starting in A1 was my intention. And for now replacing existing table is good.
The code works perfectly! Thanks!

I'm looking to expand the scope of this code.

First question:

Is it possible to select that same row of data (rather than having it start in "A1") and run the macro?

Second question:

My source table will actually be spanning quite a few columns with the SampleID/Depths to be split, and creating several tables from the source table
Example Source table:




GP-6-1.8
GP-6-5.5GP-6-18.25GP-7-1.8GP-7-5.5GP-7-18.25GP-8-1.8GP-8-5.5GP-8-18.25
Benzene0.008440.00740.007490.008440.00740.007490.008440.00740.00749
Toluene0.02420.02120.02140.02420.02120.02140.02420.02120.0214
Ethylbenzene0.03020.02650.02680.03020.02650.02680.03020.02650.0268
mp-Xylene0.06040.05290.05360.06040.05290.05360.06040.05290.0536
o-Xylene0.03020.02650.02680.03020.02650.02680.03020.02650.0268
Gasoline61.04500100061.04500100061.045001000
PCE6.045.295.366.045.295.366.045.295.36

<tbody>
</tbody>














Output Tables (could be in the same worksheet, but creating a new worksheet would be slick!):

GP-6
1.85.518.25
Benzene0.00844 U0.0074 U0.00749 U
Toluene0.0242 U0.0212 U0.0214 U
Ethylbenzene0.0302 U0.0265 U0.0268 U
mp-Xylene0.0604 U0.0529 U0.0536 U
o-Xylene0.0302 U0.0265 U0.0268 U
Gasoline61.045001000
PCE6.045.295.36

<tbody>
</tbody>













GP-7
1.85.518.25
Benzene0.00844 U0.0074 U0.00749 U
Toluene0.0242 U0.0212 U0.0214 U
Ethylbenzene0.0302 U0.0265 U0.0268 U
mp-Xylene0.0604 U0.0529 U0.0536 U
o-Xylene0.0302 U0.0265 U0.0268 U
Gasoline61.045001000
PCE6.045.295.36

<tbody>
</tbody>













These could span many columns and in some cases there would be 3 depths where samples were collected, or 5 depths, or only 1 depth. So the unique primary key for a sample ID would be the first characters GP-6, GP-7, GP-8, etc.

Of course, I could just cut and paste each set of Sample ID/Depths and corresponding lab results since in most cases that can work fine as there may not be too many samples collected, but if I had a huge set of data where there were 30 or 60 sample locations collected with 5-6 depths, that would be a lot of cutting and pasting.

That all said, the code you showed is great and will save a lot of editing those columns.

Thanks again, you've helped me in the past. It's always greatly appreciated. And I learn a lot and am at least able to understand the code and can modify further. It's just that creating from scratch is one of my weak points.

stb-Russell

Hmmm...my formatting of my post is a bit skewed. To much white space between tables, and a third table (GP-8 didn't upload)...
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,937
Members
449,094
Latest member
teemeren

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