Louis_Guionneau
New Member
- Joined
- Nov 12, 2019
- Messages
- 28
Hi Everyone,
Thanks for this awesome forum. I've spent a lot of time on here in the past and am normally able to get answers from previous posts but this time I can't so trying to post myself.
I want to write a macro that takes the values of a set of columns (the number of columns will vary depending on the dataset) and paste all the values of the individual rows in column A. For example, if, in a given dataset, column A has 50 rows of data; column B has 30 rows of data; column C has 60 rows of data, then i would want the macro to update column A with values from source A1 - 50; then B1 - B30; then C1 -60 (and also delete the data in columns B and C). So column A would now have 140 rows of data, with the values that were previously in Column A listed first, then values in Column B, then values in Column C. I hope this is somewhat clear.
I've been struggling for a while to write something that works and will dynamically and automatically loop through all the columns and rows in a given dataset. I've started by coming up with the below but it doesn't work (I'm still a beginner at VBA, I know there are issues with the below, warning you in advance ).
I'd really appreciate any guidance you all can offer.
Thank you
_____________
Sub StackColumns()
Dim LastColumn As Long
LastColumn = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
Dim i As Long
For i = 2 To LastColumn
Dim CopyDestinationRow As Long
CopyDestinationRow = Cells(1, 1).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Dim LastRow As Long
LastRow = Cells(1, i).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Range(Cells(1, i), Cells(LastRow, i)).Copy Cells(CopyDestination, 1)
Next
End Sub
Thanks for this awesome forum. I've spent a lot of time on here in the past and am normally able to get answers from previous posts but this time I can't so trying to post myself.
I want to write a macro that takes the values of a set of columns (the number of columns will vary depending on the dataset) and paste all the values of the individual rows in column A. For example, if, in a given dataset, column A has 50 rows of data; column B has 30 rows of data; column C has 60 rows of data, then i would want the macro to update column A with values from source A1 - 50; then B1 - B30; then C1 -60 (and also delete the data in columns B and C). So column A would now have 140 rows of data, with the values that were previously in Column A listed first, then values in Column B, then values in Column C. I hope this is somewhat clear.
I've been struggling for a while to write something that works and will dynamically and automatically loop through all the columns and rows in a given dataset. I've started by coming up with the below but it doesn't work (I'm still a beginner at VBA, I know there are issues with the below, warning you in advance ).
I'd really appreciate any guidance you all can offer.
Thank you
_____________
Sub StackColumns()
Dim LastColumn As Long
LastColumn = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
Dim i As Long
For i = 2 To LastColumn
Dim CopyDestinationRow As Long
CopyDestinationRow = Cells(1, 1).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Dim LastRow As Long
LastRow = Cells(1, i).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Range(Cells(1, i), Cells(LastRow, i)).Copy Cells(CopyDestination, 1)
Next
End Sub