Document will not Sort properly

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
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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