Append data on the first 4 columns with the adjacent columns

whyjaydee

New Member
Joined
Feb 26, 2021
Messages
24
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone,

I basically need help making this happen.
Is it possible to append the data on my columns A B C D with the data on the columns E F G H and so on.

Basically, the data looks like this when extracted (sample image attached)

I'd like to keep the headers in row 1 and add another column for the date which is currently on row 2 and will have a Date as header.
Each column under Security should append the Security column on column A and so on and basically end up with a total of 5 columns (Security, Low End of Risk Range, High end of Risk Range, Recent Price and Date)

I hope it makes sense
 

Attachments

  • sample.JPG
    sample.JPG
    95.9 KB · Views: 7
  • 1660212847733.png
    1660212847733.png
    2.8 KB · Views: 7

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Better for next time:
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.

See if this does what you want. Test with a copy of your data.

VBA Code:
Sub StackColumns()
  Dim c As Long
  Dim rng As Range
  
  Application.ScreenUpdating = False
  Columns("A:E").Insert
  For c = 6 To 5 + Range("F1").CurrentRegion.Columns.Count Step 4
    Set rng = Range(Cells(3, c), Cells(Rows.Count, c).End(xlUp)).Resize(, 4)
    rng.Copy Destination:=Range("A" & Rows.Count).End(xlUp).Offset(1)
    Range("E" & Rows.Count).End(xlUp).Offset(1).Resize(rng.Rows.Count).Value = Cells(2, c).Value
  Next c
  Range("F1").Resize(, 4).Copy Destination:=Range("A1")
  Range("E1").Value = "Date"
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Better for next time:
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.

See if this does what you want. Test with a copy of your data.

VBA Code:
Sub StackColumns()
  Dim c As Long
  Dim rng As Range
 
  Application.ScreenUpdating = False
  Columns("A:E").Insert
  For c = 6 To 5 + Range("F1").CurrentRegion.Columns.Count Step 4
    Set rng = Range(Cells(3, c), Cells(Rows.Count, c).End(xlUp)).Resize(, 4)
    rng.Copy Destination:=Range("A" & Rows.Count).End(xlUp).Offset(1)
    Range("E" & Rows.Count).End(xlUp).Offset(1).Resize(rng.Rows.Count).Value = Cells(2, c).Value
  Next c
  Range("F1").Resize(, 4).Copy Destination:=Range("A1")
  Range("E1").Value = "Date"
  Application.ScreenUpdating = True
End Sub
Hi Peter,

Thank you for letting me know about the XL2BB tool and where to test it.

The code seems to be working fine, I just need additional code maybe where it should delete all other columns after copying all the data leaving just the 5 columns and the rest should be empty.

Thanks!
 
Upvote 0
I just need additional code maybe where it should delete all other columns after copying all the data leaving just the 5 columns and the rest should be empty.

Rich (BB code):
Sub StackColumns()
  Dim c As Long
  Dim rng As Range
  
  Application.ScreenUpdating = False
  Columns("A:E").Insert
  For c = 6 To 5 + Range("F1").CurrentRegion.Columns.Count Step 4
    Set rng = Range(Cells(3, c), Cells(Rows.Count, c).End(xlUp)).Resize(, 4)
    rng.Copy Destination:=Range("A" & Rows.Count).End(xlUp).Offset(1)
    Range("E" & Rows.Count).End(xlUp).Offset(1).Resize(rng.Rows.Count).Value = Cells(2, c).Value
  Next c
  Range("F1").Resize(, 4).Copy Destination:=Range("A1")
  Range("E1").Value = "Date"
  ActiveSheet.UsedRange.Offset(, 5).EntireColumn.Delete
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
I tried editing the code and change to .cut instead of .copy but it leaves all the dates and the headers from other columns.
 
Upvote 0
Rich (BB code):
Sub StackColumns()
  Dim c As Long
  Dim rng As Range
 
  Application.ScreenUpdating = False
  Columns("A:E").Insert
  For c = 6 To 5 + Range("F1").CurrentRegion.Columns.Count Step 4
    Set rng = Range(Cells(3, c), Cells(Rows.Count, c).End(xlUp)).Resize(, 4)
    rng.Copy Destination:=Range("A" & Rows.Count).End(xlUp).Offset(1)
    Range("E" & Rows.Count).End(xlUp).Offset(1).Resize(rng.Rows.Count).Value = Cells(2, c).Value
  Next c
  Range("F1").Resize(, 4).Copy Destination:=Range("A1")
  Range("E1").Value = "Date"
  ActiveSheet.UsedRange.Offset(, 5).EntireColumn.Delete
  Application.ScreenUpdating = True
End Sub
Works perfectly!! Amazing! Thank you so much Peter.
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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