简体   繁体   中英

How can I sort rows in Excel alphabetically, without including all columns in the sort?

Trying to sort data in Excel alphabetically by row, but NOT in all of the columns for each row.

For example: Sort J7 through U7 alphabetically, then J8 through U8, all the way down through J2000 through U2000, while keeping each item within its row (like all cells in row 7 should still be in row 7 by the end of it.)

It seems like I'll need to use a macro/VBA for this, as Excel only lets you sort one row at a time alphabetically. If I select more than one row to sort, it still only sorts the first row.

I have done a bit of research as far as what kind of macros I could use for this, but nothing seems to do the job. The one thing I found that was similar to this sorts by number, but can't sort alphabetically.

Here's what I've got at a moment. I recorded a macro of myself selecting and sorting the row so that I could hopefully get all the code for that right at least:

Sub Macro4()
'
' Macro4 Macro
'
' Keyboard Shortcut: Option+Cmd+j
'

Dim lngIndex As Long
Dim strArray(9 To 11000) As String
Dim intCounter As Integer
Dim x As Integer
intCounter = 1
x = 9
For lngIndex = LBound(strArray) To UBound(strArray)
intCounter = intCounter + 1
strArray(lngIndex) = intCounter
x = x + 1
Range("Jx:UNx").Select
ActiveWorkbook.Worksheets("export_729559 (3).csv").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("export_729559 (3).csv").Sort.SortFields.Add Key:= _
Range("Jx:UNx"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("export_729559 (3).csv").Sort
.SetRange Range("Jx:UNx")
.Header = xlNo
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
Next

End Sub

The error I'm getting right now is "Compile Error: Next Without For" for that "Next" at the bottom. But obviously there is a For higher up, and as far as I know they should be linked unless the "With" in between is messing things up. I've tried Next on it's own like it is above, as well as "Next IngIndex".

Your With block doesn't have an End. Try like this:

With ActiveWorkbook.Worksheets("export_729559 (3).csv").Sort
.SetRange Range("Jx:UNx")
.Header = xlNo
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With 'I have added this line to close your With block
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM