Turn rows and columns into one row using VBA

Technology

New Member
Joined
Apr 29, 2021
Messages
25
Office Version
  1. 365
Platform
  1. 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?
 
Give this macro a try...
VBA Code:
Sub CopySingleDataRowAndSortIt()
  Dim Data As Range
  Set Data = Range("A1", Cells(1, Columns.Count).End(xlToLeft))
  Data.Copy Data(1).Offset(1)
  With Data.Parent.Sort
    .SortFields.Clear
    .SortFields.Add2 Key:=Data.Offset(1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .SetRange Data.Offset(1)
    .Orientation = xlLeftToRight
    .Apply
  End With
End Sub
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Give this macro a try...
VBA Code:
Sub CopySingleDataRowAndSortIt()
  Dim Data As Range
  Set Data = Range("A1", Cells(1, Columns.Count).End(xlToLeft))
  Data.Copy Data(1).Offset(1)
  With Data.Parent.Sort
    .SortFields.Clear
    .SortFields.Add2 Key:=Data.Offset(1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .SetRange Data.Offset(1)
    .Orientation = xlLeftToRight
    .Apply
  End With
End Sub

Yes, this works. Thank you! It now requires another separate macro that finds the median of the ordered numbers and place that value in Row 3, Column A. The median should be 4.
 
Upvote 0
Rick, are you able to help with finding the median of the order row. I have only been successful when the entries are in a column. Thank you.
 
Upvote 0
finding the median of the order row
Tip: you need to know how many numbers/cells there are (this you know).
if it's odd, divide by 2, round it up. Get the value of the cell in this column's row 2.
if it's even, divide by 2, get the values of the cell in this column's row 2 + the cell in the next column, sum them and divide by 2.
 
Upvote 0
I now need to find the occurrence (mode) of the value 10, and place that result in A4.
 
Upvote 0
Yes, this works. Thank you!
@Technology - I can see that @Rick Rothstein's code helped you in this question.
I marked the solution post in this question for you. Next time, when you receive an answer that helped to solve your question, please mark that post as the solution to the question, so future readers might access the solution quickly. Thanks.
 
Upvote 0
@smozgur Ok, I see how to do that now. Thank you! I want to be a productive member, and hope that one day I can assist others.
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,657
Members
449,462
Latest member
Chislobog

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