Data Flipping in a row - Write in reverse order
Flipping data in a row to write it in reverse order.
Let's say you have data in a row and you want to have the data in reverse order.
You can do that in Excel by writing the VBA code.
Assumption: The start position of data is range A1.
Steps:
1. Open a new file
2. Press Alt+F11 to open VBA editor
3. In the left navigation value select ThisWorkbook
4. In the editor window enter the code below
5. Close the VBA editor
6. Save the file as Macro Enabled File
7. Test with you inputs
Code:
Sub FlipDataInARow()
Dim oldStart As Variant
Dim oldEnd As Variant
Dim newStart As Integer
Dim newEnd As Integer
Range("A1").Select 'Change the Start position here
Range(Selection, Selection.End(xlToRight)).Select
Application.ScreenUpdating = False
newStart = 1
newEnd = Selection.Columns.Count
Do While newStart < newEnd
oldStart = Selection.Columns(newStart)
oldEnd = Selection.Columns(newEnd)
Selection.Columns(newEnd) = oldStart
Selection.Columns(newStart) = oldEnd
newStart = newStart + 1
newEnd = newEnd - 1
Loop
Application.ScreenUpdating = True
End Sub
The above code will work if the data is continuous, if your data is not continuous you need to replace the below line of code.
Continuous data: Range(Selection, Selection.End(xlToRight)).Select
Non Continuous data: Range(Selection, Cells(Selection.Row, Columns.Count).End(xlToLeft)).Select
ScreenShot of data before running code:
ScreenShot of data after running code:
I think this has been informative and I thank you for viewing.
-Saurabh
Attachment | Size |
---|---|
FlipDataInARow.xlsm | 15.59 KB |
- saurabhlakhanpal's blog
- Login or register to post comments
- 7806 reads
faster
Option Base 1
Sub FlipDataInARow2()
Dim rRange As Range
Dim myArray()
Dim myArray2()
Set rRange = Range(Selection, Cells(Selection.Row, Columns.Count).End(xlToLeft))
myArray = rRange
ReDim myArray2(rRange.Columns.Count)
j = 1
For i = UBound(myArray, 2) To LBound(myArray, 2) Step -1
myArray2(j) = myArray(1, i)
j = j + 1
Next i
rRange = myArray2
Application.ScreenUpdating = True
End Sub