Technology
New Member
- Joined
- Apr 29, 2021
- Messages
- 25
- Office Version
- 365
- Platform
- Windows
How do you take a set of values (rows and columns) in EXCEL and use VBA to puts all those values into one row?
7 | 3 | 8 | 6 | 3 | 6 |
1 | 6 | 9 | 4 | 2 | 5 |
5 | 10 | 10 | 4 | 6 | 2 |
1 | 7 | 6 | 7 | 0 | 6 |
1 | 5 | 6 | 3 | 9 | 7 |
1 | 7 | 8 | 8 | 1 | 6 |
2 | 8 | 0 | 6 | 0 | 2 |
7 | 5 | 10 | 1 | 3 | 0 |
8 | 5 | 9 | 9 | 4 | 0 |
6 | 4 | 3 | 2 | 4 | 2 |
8 | 4 | 7 | 6 | 6 | 9 |
6 | 4 | 1 | 4 | 3 | 1 |
2 | 3 | 0 | 8 | 8 | 4 |
0 | 3 | 5 | 4 | 7 | 6 |
7 | 1 | 0 | 0 | 8 | 3 |
4 | 1 | 8 | 3 | 9 | 6 |
4 | 1 | 1 | 10 | 3 | 5 |
7 | 3 | 0 | 4 | 1 | 4 |
3 | 6 | 6 | 9 | 6 | 1 |
8 | 2 | 3 | 6 | 6 | 8 |
0 | 0 | 1 | 10 | 1 | 2 |
5 | 2 | 9 | 4 | 3 | 7 |
5 | 7 | 3 | 8 | 1 | 6 |
Sub MultipleRowsToOneRow()
Dim R As Long, C As Long, StartRow As Long, LastRow As Long
Dim Arr As Variant, Result As Variant
Arr = Range("A1").CurrentRegion
ReDim Result(1 To UBound(Arr, 1), 1 To UBound(Arr, 1) * UBound(Arr, 2))
For R = 1 To UBound(Arr, 1)
For C = 1 To UBound(Arr, 2)
Result(1, (R - 1) * UBound(Arr, 2) + C) = Arr(R, C)
Next
Next
Range("A1").Resize(UBound(Result, 1), UBound(Result, 2)) = Result
End Sub
Thank you for the kind words of advice and patience. I don't want to ruffle any feathers, and will do better in providing data. Thank you for your help with the code as well. I took the time to understand and learn from it. Could you also help me with creating one VBA program to order the numbers in the row we you just did and put them in another row (say row 2). Again, I really appreciate it.Assuming your data starts at cell A1, does this macro do what you want?
VBA Code:Sub MultipleRowsToOneRow() Dim R As Long, C As Long, StartRow As Long, LastRow As Long Dim Arr As Variant, Result As Variant Arr = Range("A1").CurrentRegion ReDim Result(1 To UBound(Arr, 1), 1 To UBound(Arr, 1) * UBound(Arr, 2)) For R = 1 To UBound(Arr, 1) For C = 1 To UBound(Arr, 2) Result(1, (R - 1) * UBound(Arr, 2) + C) = Arr(R, C) Next Next Range("A1").Resize(UBound(Result, 1), UBound(Result, 2)) = Result End Sub
Please Note
-------------------
One thing you must keep in mind when you ask a question in a forum... the people you are asking to help you know absolutely nothing about your data, absolutely nothing about how it is laid out in the workbook, absolutely nothing about what you want done with it and absolutely nothing about how whatever it is you want done is to be presented back to you as a result... you must be very specific about describing each of these areas, in detail, and you should not assume that we will be able to "figure it out" on our own. Remember, you are asking us for help... so help us to be able to help you by providing the information we need to do so, even if that information seems "obvious" to you (remember, it is only obvious to you because of your familiarity with your data). To sum up... we only know what you tell us, nothing more.
I am not 100% clear on what you want here. Are you asking to add to the macro I posted code with copies the single row that the code just created and then sort that second row into numerical order? If so, what sort order do you want... lowest number on left, highest number on the right or vice versa? Also, should duplicate numbers be kept retained or collapsed down to a single entry?Could you also help me with creating one VBA program to order the numbers in the row we you just did and put them in another row (say row 2). Again, I really appreciate it.