Stack columns but carry row data

COWater

New Member
Joined
Sep 1, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I need help taking a table with multiple columns of water quality data and stacking a subset of the columns into one column, but carrying the associated row data like project, Location, latitude, etc.

Example table below.

1693585097714.png


You can see that our data is arranged by sampling event, with multiple parameters (columns) starting with pH. I need to take all of the parameters and stack them into a single column BUT each sample parameter needs to carry the row data with it, from Project column through Field Technician column.

I was lucky to find VBA code on this site that allows me to do exactly what I am asking, but the only one column of data is carried forward with each parameter. For example, I can stack all of the parameters (and leave skip blank cells) but only carrying over the "Project" column.

Example of the code I'm using is here:

1693585370693.png


Any help would be greatly appreciated. I'm happy to clarify anything that may not be clear.

THANKS!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi @COWater, Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

Note XL2BB:
It would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.​
It is important to put the raw data and also the expected result.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.​

Note Code Tag:
In future please use code tags when posting code.​
How to Post Your VBA Code it makes your code easier to read and copy and it also maintains VBA formatting.​

--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------
 
Upvote 0
Absolutely, thanks for these great resources. This is my first post (probably first of many), so please excuse my newbi-"ness" :)

Here is the XL2BB you reference above:

Brads_work.xlsx
BCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAP
2ProjectLocationLatitudeLongitudeSample TypeSample DateSample TimeSeason Field TechnicianpHWater Temperature (°F)Water Temperature (°C)Specific Conductance (µS/cm)TDS (mg/L)pHCarbonateCalciumMagnesiumPotassiumSodiumNitrate+NitriteTotal PhosphorusTotal SulfurChlorideIronManganeseZincCopperBoronTotal HardnessLimeSARSARadjTotal Kjeldahl NitrogenSulfateDissolved ArsenicDissolved SeleniumDissolved UraniumTotal ArsenicTotal SeleniumTotal Uranium
3319 - Buffer ZoneBuffer Zone Drain37.982151-103.647266Drain6/12/20182:40 PMIrrigationC. Roweth8.8231.8876570
4319 - Buffer ZoneBuffer Zone Drain37.982151-103.647266Drain8/2/20199:54 AMC. Roweth7.3425.2760493
5319 - Buffer ZoneBuffer Zone Drain37.982151-103.647266Drain5/19/20209:06 AMDrainC. Roweth7.9820.9597420
6319 - Buffer ZoneBuffer Zone Drain37.982151-103.647266Drain6/2/20209:12 AMDrainC. Roweth8.1321779548
7319 - Buffer ZoneBuffer Zone Drain37.982151-103.647266Drain6/15/202010:48 AMDrainC. Roweth8.0125.3886573
8319 - Buffer ZoneBuffer Zone Irrigation37.977932-103.650757Irrigation6/12/20182:32 PMIrrigationC. Roweth8.4530.2804523
9319 - Buffer ZoneBuffer Zone Irrigation37.977932-103.650757Irrigation7/12/201810:50 AMIrrigationC. Roweth8.2725.47554917.9088263441.160.1395.3170.130.410.0100.0819.095441.12.41.96283.0500.0070.0090.0050.0090.0098
10319 - Buffer ZoneBuffer Zone Irrigation37.977932-103.650757Tailwater7/12/201810:30 AMIrrigationC. Roweth7.7519.48165307.4095289460.730.5997.6210.010.390.0200.1220.595221.12.51.96289.910.0040.0110.00750.0030.0080.0075
11319 - Buffer ZoneBuffer Zone Irrigation37.977932-103.650757Irrigation7/27/201811:15 AMIrrigationD. Oneal8.0524.7734477
12319 - Buffer ZoneBuffer Zone Irrigation37.977932-103.650757Tailwater7/27/201811:20 AMIrrigationD. Oneal8.0930.4760493
13319 - Buffer ZoneBuffer Zone Irrigation37.977932-103.650757Irrigation8/24/201811:55 AMIrrigationC. Roweth8.3327.91362881
14319 - Buffer ZoneBuffer Zone North38.998961-103.637311Well8/13/201810:55 AMIrrigationD. Oneal6.9820.8304819837.105348611016.560567.8370.0100.0800.2798.569411.13.32.011686.4000.010.05890.0390.0110.0685
15319 - Buffer ZoneBuffer Zone North38.998961-103.637311Well8/24/201811:37 AMIrrigationC. Roweth7.2221.732592123
16319 - Buffer ZoneBuffer Zone North38.998961-103.637311Well9/21/20183:12 PMIrrigationC. Roweth7.0320.833982208
17319 - Buffer ZoneBuffer Zone North38.998961-103.637311Well6/17/20192:10 PMIrrigationC. Roweth7.7516.130641981
18319 - Buffer ZoneBuffer Zone North38.998961-103.637311Well7/18/201910:30 AMIrrigationC. Roweth7.5517.6301519600.0070.0050.04890.060.0290.0773
19319 - Buffer ZoneBuffer Zone North38.998961-103.637311Well8/2/20199:47 AMC. Roweth6.9329.119101892
20319 - Buffer ZoneBuffer Zone North38.998961-103.637311Well5/7/202012:53 PMWellM. Weber7.341736303141
21319 - Buffer ZoneBuffer Zone North38.998961-103.637311Well5/19/20209:03 AMWellC. Roweth7.5412.628982457
22319 - Buffer ZoneBuffer Zone North38.998961-103.637311Well6/3/20208:48 AMWellC. Roweth7.651541123320
23319 - Buffer ZoneBuffer Zone North38.998961-103.637311Well6/15/202010:32 AMWellC. Roweth7.1315.442673347
24319 - Buffer ZoneBuffer Zone North38.998961-103.637311Well6/26/202012:35 PMWellC. Roweth7.8216.743263344
25319 - Buffer ZoneBuffer Zone North38.998961-103.637311Well7/14/20209:13 AMWellC. Roweth7.1919.4450373274
26319 - Buffer ZoneBuffer Zone South37.978063-103.648274Well8/7/201810:55 AMIrrigationD. Oneal6.9317.863584132
27319 - Buffer ZoneBuffer Zone South37.978063-103.648274Well8/13/201811:00 AMIrrigationD. Oneal6.9617.7688844777.1053021519250.0101203.53670.0100.0901.54128.9916568.528.61.013574.4800.0170.06160.0120.0120.0604
28319 - Buffer ZoneBuffer Zone South37.978063-103.648274Well8/24/201811:43 AMIrrigationC. Roweth7.0518.764794212
29319 - Buffer ZoneBuffer Zone South37.978063-103.648274Well9/21/20183:15 PMIrrigationC. Roweth7.118.660493951
30319 - Buffer ZoneBuffer Zone South37.978063-103.648274Well6/17/20192:15 PMIrrigationC. Roweth7.614.650953304
31319 - Buffer ZoneBuffer Zone South37.978063-103.648274Well7/18/201910:30 AMIrrigationC. Roweth7.217.3503032700.0030.0220.05070.0760.0280.0783
32319 - Buffer ZoneBuffer Zone South37.978063-103.648274Well8/2/20199:49 AMC. Roweth6.1818.152935423
33319 - Lateral LiningMcClave 138.143266-102.851549Well6/1/20181:04 PMIrrigationD. Oneal6.9415.636892027
34319 - Lateral LiningMcClave 138.143266-102.851549Well6/1/20181:04 PMIrrigationM. Weber6.9415.636892027
35319 - Lateral LiningMcClave 138.143266-102.851549Well6/4/201810:20 AMIrrigationD. Oneal7.1815353022977.20376150936500677.21030.120.580.0200.7290.91,106412.42.942,004.64
36319 - Lateral LiningMcClave 138.143266-102.851549Well6/22/201810:25 AMIrrigationC. Roweth6.9116.527941816
37319 - Lateral LiningMcClave 138.143266-102.851549Well6/25/20182:40 PMIrrigationC. Roweth7.0416.528001824
38319 - Lateral LiningMcClave 138.143266-102.851549Well7/5/20183:06 PMIrrigationB. Brubacher6.917.526841742
39319 - Lateral LiningMcClave 138.143266-102.851549Well7/10/20182:00 PMIrrigationC. Roweth6.8718.727931815000.05640.0220.0050.0618
Sheet3
 
Upvote 0
And here is the code in proper format (I'm not sure if this code is really what I'm after, so feel free to modify, edit, or delete and rework something new).

VBA Code:
Sub stackcolumn_v2()

Dim a As Variant, lr As Long, lc As Long, n As Long, r As Long, c As Long
Dim o As Variant, j As Long
Application.ScreenUpdating = False
With ActiveSheet
  lr = .Cells(.Rows.Count, 1).End(xlUp).Row
  lc = .Cells(1, .Columns.Count).End(xlToLeft).Column
  a = .Range(.Cells(1, 1), .Cells(lr, lc))
  n = Application.CountA(.Range(.Cells(2, 2), .Cells(lr, lc))) + 1
  ReDim o(1 To n, 1 To 3)
  j = j + 1: o(j, 1) = "": o(j, 2) = ""
  For r = 3 To lr
    For c = 11 To lc
      If Not a(r, c) = vbEmpty Then
        j = j + 1: o(j, 1) = a(r, 1)
        o(j, 2) = a(r, c)
      End If
    Next c
  Next r
  .Cells(1, lc + 2).Resize(UBound(o, 1), UBound(o, 2)) = o
  .UsedRange.Columns.AutoFit
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Cowater and thanks for the example in the minisheet.

I propose the following:
1. Create a new sheet and name it "Result" to put the result.​
2. In the results sheet, I added a column to put the name of the parameter (column J) and in the next column the value of the parameter (column K).​
If you don't want the name of the parameter, change these lines:​
b(m, 10) = a(1, j)
b(m, 11) = a(i, j)
By this line:​
b(m, 10) = a(i, j)
----- --

Put the following code in a module.
Don't forget to adjust the sheet names in the code.

VBA Code:
Sub stackcolumn_v3()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim a As Variant, b As Variant
  Dim i&, j&, k&, lr&, lc&, n&, m&
 
  Set sh1 = Sheets("Raw")       'Fit to your sheets's name
  Set sh2 = Sheets("Result")    'New sheet
 
  lr = sh1.Cells(Rows.Count, 1).End(xlUp).Row
  lc = sh1.Cells(1, Columns.Count).End(xlToLeft).Column
  a = sh1.Range("A1", sh1.Cells(lr, lc)).Value
  n = Application.CountA(sh1.Range("J2", sh1.Cells(lr, lc))) + 1
  ReDim b(1 To n, 1 To 11)
 
  For i = 2 To UBound(a, 1)
    For j = 10 To UBound(a, 2)
      If a(i, j) <> "" Then
        m = m + 1
        For k = 1 To 9
          b(m, k) = a(i, k)
        Next
        b(m, 10) = a(1, j)
        b(m, 11) = a(i, j)
      End If
    Next j
  Next i
 
  sh2.Cells.ClearContents
  sh2.Range("A1:I1").Value = sh1.Range("A1:I1").Value
  sh2.Range("J1:K1").Value = Array("Parameter", "Value")
  sh2.Range("A2").Resize(UBound(b, 1), UBound(b, 2)) = b
  sh2.UsedRange.Columns.AutoFit
End Sub

Run the macro, you will see the results in the "result" sheet

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,215,108
Messages
6,123,133
Members
449,098
Latest member
Doanvanhieu

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