General Ledger
Active Member
- Joined
- Dec 31, 2007
- Messages
- 460
Dear All,
I am looking for VBA that will sort columns based on values I assign to labels of the columns. I use this on data where I want columns to be in a certain order.
I have code that:
This code can become quite long if I have many columns of data. It seems like there should be an easier way. Any ideas? Maybe something with Arrays?
Thanks,
GL
I am looking for VBA that will sort columns based on values I assign to labels of the columns. I use this on data where I want columns to be in a certain order.
I have code that:
- Inserts a blank row above the column labels
- Puts a value in the blank cell above the column label
- Sorts the columns based on the values
- Deletes the row of values
Code:
'Insert row in which to enter sequence numbers
Rows("1:1").Insert Shift:=xlDown
'Assign sequence numbers to columns
Range("A2").Select
Do Until ActiveCell = Empty
Select Case ActiveCell.Value
Case "Supplier"
ActiveCell.Offset(-1, 0) = 10
ActiveCell.Offset(0, 1).Select
Case "Invoice Num"
ActiveCell.Offset(-1, 0) = 20
ActiveCell.Offset(0, 1).Select
Case "Invoice Amount"
ActiveCell.Offset(-1, 0) = 30
ActiveCell.Offset(0, 1).Select
Case Else
ActiveCell.EntireColumn.Delete
End Select
Loop
'Sort columns
Cells.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight, _
DataOption1:=xlSortNormal
'Delete sequence number row
Rows("1:1").Delete Shift:=xlUp
This code can become quite long if I have many columns of data. It seems like there should be an easier way. Any ideas? Maybe something with Arrays?
Thanks,
GL