Hey all-
I am attempting to "Sort" data based on the label/heading in the first row (i.e. Account, then by Type).
[TABLE="width: 988"]
<tbody>[TR]
[TD][TABLE="width: 1081"]
<tbody>[TR]
[TD]Type[/TD]
[TD]Date[/TD]
[TD]Num[/TD]
[TD]Name[/TD]
[TD]Memo[/TD]
[TD]Account[/TD]
[TD]Debit[/TD]
[TD]Credit[/TD]
[TD]Balance[/TD]
[/TR]
[TR]
[TD]Bill[/TD]
[TD]0/16/2014[/TD]
[TD]111111[/TD]
[TD]John[/TD]
[TD]John company[/TD]
[TD]10000 · A/R - xx[/TD]
[TD][/TD]
[TD="align: right"]150.00[/TD]
[TD="align: right"]124,780.23[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
This is the code when I recorded the macro:
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("E2:F=E100000" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2:A100000" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:Z100000")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
The problem is that those columns are not always in the same column order when I generate the data. The macro Sorts by column letter (i.e. column F and A) instead of by the column name. Does anyone know how to Sort by finding the labels in the first column and Sorting by that column despite the fact that the column may be in a different location on the spreadsheet.
Also, if I generate a report and it does not contain that name title (i.e. Account), then to do nothing and not sort and move to the next Sort column (i.e. Type). Is that even possible?
... Later in the code, I use the Subtotal feature to sum the Debit and Credit columns (another spot I have to find the column letter/number again) at every change in Account (column E or 6th column) number, and then Subtotal again by a change in Type (column A or column 1).
My code looks like the following:
Selection.Subtotal GroupBy:=6, Function:=xlSum, TotalList:=Array(11, 12), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(11, 12), _
Replace:=False, PageBreaks:=False, SummaryBelowData:=True
Am what I trying to do feasible???
Thank you in advance for your input!
I am attempting to "Sort" data based on the label/heading in the first row (i.e. Account, then by Type).
[TABLE="width: 988"]
<tbody>[TR]
[TD][TABLE="width: 1081"]
<tbody>[TR]
[TD]Type[/TD]
[TD]Date[/TD]
[TD]Num[/TD]
[TD]Name[/TD]
[TD]Memo[/TD]
[TD]Account[/TD]
[TD]Debit[/TD]
[TD]Credit[/TD]
[TD]Balance[/TD]
[/TR]
[TR]
[TD]Bill[/TD]
[TD]0/16/2014[/TD]
[TD]111111[/TD]
[TD]John[/TD]
[TD]John company[/TD]
[TD]10000 · A/R - xx[/TD]
[TD][/TD]
[TD="align: right"]150.00[/TD]
[TD="align: right"]124,780.23[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
This is the code when I recorded the macro:
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("E2:F=E100000" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2:A100000" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:Z100000")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
The problem is that those columns are not always in the same column order when I generate the data. The macro Sorts by column letter (i.e. column F and A) instead of by the column name. Does anyone know how to Sort by finding the labels in the first column and Sorting by that column despite the fact that the column may be in a different location on the spreadsheet.
Also, if I generate a report and it does not contain that name title (i.e. Account), then to do nothing and not sort and move to the next Sort column (i.e. Type). Is that even possible?
... Later in the code, I use the Subtotal feature to sum the Debit and Credit columns (another spot I have to find the column letter/number again) at every change in Account (column E or 6th column) number, and then Subtotal again by a change in Type (column A or column 1).
My code looks like the following:
Selection.Subtotal GroupBy:=6, Function:=xlSum, TotalList:=Array(11, 12), _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(11, 12), _
Replace:=False, PageBreaks:=False, SummaryBelowData:=True
Am what I trying to do feasible???
Thank you in advance for your input!