Brian Feth
New Member
- Joined
- May 21, 2017
- Messages
- 37
Windows 11, Excel 2021; I have a problem with the Sort function. I put together a macro that takes the .csv bank account information and compiles into one document for manipulation. The columns are; Date, Transaction, Debit, Credit, Vendor_1 (e.g. “Home Depot, Costco, etc.), Catigory_1 (e.g. Medical, Food, etc.) , and Description (the banks description of the exchange). The problem; it won’t Sort. When I Sort by Vendor_1 it goes through a sort process but the result is garbled. I end up with 1 to 4 entries of one record over writing the actual entries followed by several correct rows followed by an several misplaced alternating repetitive rows. The document has ~500 rows.
It all seems pretty straight forward but it doesn’t work. Any ideas? Help is always appreciated.
Sub SortByVendor()
WBName = Range("N1").Value
Range("N2").Value = Range("P2").Value
LedgerName = Range("N2").Value
Workbooks(WBName).Worksheets(LedgerName).Activate
Range("C5").Select
Selection.End(xlDown).Select
TopRange = Selection.Row
Set SortRange = Range("C" & TopRange & ":" & "J1000")
Set KeyRange = Range("H" & TopRange & ":" & "H1000")
ActiveWorkbook.Worksheets(LedgerName).Sort.SortFields.Clear
ActiveWorkbook.Worksheets(LedgerName).Sort.SortFields.Add2 Key:=KeyRange, _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets(LedgerName).Sort 'DataOption1 Optional XlSortDataOption
.SetRange SortRange
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
'.SortMethod = xlPinYin
.XlSortDataOption = 0
.Apply
End With
End Sub
It all seems pretty straight forward but it doesn’t work. Any ideas? Help is always appreciated.
Sub SortByVendor()
WBName = Range("N1").Value
Range("N2").Value = Range("P2").Value
LedgerName = Range("N2").Value
Workbooks(WBName).Worksheets(LedgerName).Activate
Range("C5").Select
Selection.End(xlDown).Select
TopRange = Selection.Row
Set SortRange = Range("C" & TopRange & ":" & "J1000")
Set KeyRange = Range("H" & TopRange & ":" & "H1000")
ActiveWorkbook.Worksheets(LedgerName).Sort.SortFields.Clear
ActiveWorkbook.Worksheets(LedgerName).Sort.SortFields.Add2 Key:=KeyRange, _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets(LedgerName).Sort 'DataOption1 Optional XlSortDataOption
.SetRange SortRange
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
'.SortMethod = xlPinYin
.XlSortDataOption = 0
.Apply
End With
End Sub