combine multiple columns keeping some columns static

lramesh

New Member
Joined
Oct 15, 2013
Messages
9
This is a sample of the data I have


I would like to have my data like this - Image titled target file


Please let me know how this can be done with or without macros.

Source data

[TABLE="width: 500"]
<tbody>[TR]
[TD]Staffname[/TD]
[TD]Empname[/TD]
[TD]EmpID[/TD]
[TD]Project1[/TD]
[TD]Domain[/TD]
[TD]Subdomain[/TD]
[TD]Project2[/TD]
[TD]Domain[/TD]
[TD]Subdomain[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]AX[/TD]
[TD]1[/TD]
[TD]WER[/TD]
[TD]WERD[/TD]
[TD]WERS[/TD]
[TD]PQR[/TD]
[TD]PQRS[/TD]
[TD]PQRA[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]BA[/TD]
[TD]2[/TD]
[TD]VAR[/TD]
[TD]VARD[/TD]
[TD]VARS[/TD]
[TD]EWR[/TD]
[TD]EWRS[/TD]
[TD]EWRA[/TD]
[/TR]
</tbody>[/TABLE]

Target data

[TABLE="width: 500"]
<tbody>[TR]
[TD]Staffname[/TD]
[TD]Empname[/TD]
[TD]EmpID[/TD]
[TD]Projects[/TD]
[TD]Domain[/TD]
[TD]Subdomain[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]AX[/TD]
[TD]1[/TD]
[TD]WER[/TD]
[TD]WERD[/TD]
[TD]WERS[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]AX[/TD]
[TD]1[/TD]
[TD]PQR[/TD]
[TD]PQRS[/TD]
[TD]PQRA[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]BA[/TD]
[TD]2[/TD]
[TD]VAR[/TD]
[TD]VARD[/TD]
[TD]VARS[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]BA[/TD]
[TD]2[/TD]
[TD]EWR[/TD]
[TD]EWRS[/TD]
[TD]EWRA[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
lramesh,

Welcome to the MrExcel Forum.

What version of Excel are you using?

I assume that worksheets Source and Target already exist.

The macro will adjust for a varying number of rows and columns in worksheet Source.

Sample worksheets:


Excel 2007
ABCDEFGHI
1StaffnameEmpnameEmpIDProject1DomainSubdomainProject2DomainSubdomain
2AAX1WERWERDWERSPQRPQRSPQRA
3BBA2VARVARDVARSEWREWRSEWRA
4
Source



Excel 2007
ABCDEF
1StaffnameEmpnameEmpIDProjectsDomainSubdomain
2
3
4
5
6
Target


After the macro using two arrays in memory in worksheet Target:


Excel 2007
ABCDEF
1StaffnameEmpnameEmpIDProjectsDomainSubdomain
2AAX1WERWERDWERS
3AAX1PQRPQRSPQRA
4BBA2VARVARDVARS
5BBA2EWREWRSEWRA
6
Target


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:
Option Explicit
Sub ReorgData()
' hiker95, 10/15/2013
' http://www.mrexcel.com/forum/excel-questions/733007-combine-multiple-columns-keeping-some-columns-static.html
Dim s As Variant, t As Variant
Dim i As Long, ii As Long, c As Long, n As Long
Dim lr As Long, lc As Long
With Sheets("Source")
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  lc = .Cells(1, Columns.Count).End(xlToLeft).Column
  s = .Range(.Cells(2, 1), .Cells(lr, lc))
  n = (((lc - 3) / 3) * UBound(s, 1))
  ReDim t(1 To n, 1 To 6)
End With
For i = 1 To UBound(s, 1)
  For c = 4 To UBound(s, 2) Step 3
    ii = ii + 1
    t(ii, 1) = s(i, 1): t(ii, 2) = s(i, 2): t(ii, 3) = s(i, 3)
    t(ii, 4) = s(i, c): t(ii, 5) = s(i, c + 1): t(ii, 6) = s(i, c + 2)
  Next c
Next i
With Sheets("Target")
  .UsedRange.ClearContents
  .Range("A1").Resize(, 6).Value = [{"Staffname","Empname","EmpID","Projects","Domain","Subdomain"}]
  .Range("A2").Resize(UBound(t, 1), UBound(t, 2)) = t
  .Columns.AutoFit
  .Activate
End With
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.
 
Last edited:
Upvote 0
Hi,

Thanks for the macro. I changed the name of source and target sheets from my file and ran the macro.

I got a run time error - Subscript out of range

I had to include a few other column names that were static.

Please let me know what can be done.

thanks
 
Upvote 0
I tried with the same sample data I had provided. It works. When I change the number of columns (I changed it to 8 static columns that will remain constant and 3 columns that will contain the combined data), it does not work. Though the macro executes, the data is not aligned with the corresponding columns.



Hi,

Thanks for the macro. I changed the name of source and target sheets from my file and ran the macro.

I got a run time error - Subscript out of range

I had to include a few other column names that were static.

Please let me know what can be done.

thanks
 
Upvote 0
lramesh,

In order to continue I will need to see your workbook.

You can upload your workbook to Box Net,
sensitive data changed
mark the workbook for sharing
and provide us with a link to your workbook.
 
Upvote 0
lramesh,

I will not be available to assist you until this coming Sunday/Monday.

If you can not wait until then:

Click on the Reply to Thread button, and just put the word BUMP in the post. Then, click on the Post Quick Reply button, and someone else will assist you.
 
Upvote 0
Re: combine multiple columns keeping some columns static [BUMP]

lramesh,

Your link is not working - please try again.
 
Upvote 0

Forum statistics

Threads
1,222,749
Messages
6,167,971
Members
452,158
Latest member
MattyM

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