这个挑战是在回答这个问题后得出的: Excel VBA Sorting
我使用了作为其中一个答案提供的代码,并尝试将其编辑为我的情况
我需要对A列,A1中的标题,A到Z进行排序 - 同时保持行完整性,即行值遵循已排序的单元格
这是我尝试过的代码编辑,但我显然没有正确编辑它,因为A列值没有像我希望的那样按字母顺序排序 - 有人可以帮忙吗?
With ws_catalogue
'''''''''''''''''''''''''''''''''''''''''''
'Get range from B1 to last cell on sheet. '
'''''''''''''''''''''''''''''''''''''''''''
Set myRange = .Range(.Cells(1, 1), .Cells(.Cells.Find("*", , , , 1, 2).Row, .Cells.Find("*", , , , 2, 2).Column))
myRange.Select
With .Sort
.SortFields.Clear
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'As using late binding Access won't understand Excel values so: '
'xlSortOnValues = 0 xlYes = 1 '
'xlAscending = 1 xlTopToBottom = 1 '
'xlSortNormal = 0 xlPinYin = 1 '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
.SortFields.Add _
Key:=myRange.Offset(, 6).Resize(, 1), _
SortOn:=0, _
Order:=1, _
DataOption:=0
.SetRange myRange
.Header = 1
.MatchCase = False
.Orientation = 1
.SortMethod = 1
.Apply
End With
End With
答案 0 :(得分:0)
如果您想按照A列上的值按升序排序,那么以下内容将考虑整个表格进行排序:
Sub sortColumnA()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
If ws.AutoFilterMode = False Then ws.Cells.AutoFilter
ws.AutoFilter.Sort.SortFields.Clear
ws.AutoFilter.Sort.SortFields.Add Key:=Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ws.AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ws.Cells.AutoFilter
End Sub
<强>更新强>
如果您只想对特定范围进行排序,那么下面的内容也应该起作用:
Sub sortColumnA()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'declare and set your worksheet, amend as required
If ws.AutoFilterMode = False Then ws.Range("A1:Z1").AutoFilter
ws.AutoFilter.Sort.SortFields.Clear
ws.AutoFilter.Sort.SortFields.Add Key:=Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ws.AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ws.Range("A1:Z1").AutoFilter
End Sub
答案 1 :(得分:0)
@Xabier您已经处理了一些其他注意事项,抱歉这段代码来自另一个类似的答案,但它定义了工作表,以及列名称以便于阅读代码。我将跳过Dims并转到它的内容,这是一个3列未知或改变行数量。该示例仅显示使用行和列设置范围定义,使用单元格来定义范围。我在排序时停止了代码,其余代码为每个人生成一封电子邮件,其中包含电子邮件正文中列出的部门名称,与此问题无关。也许是另一种思考方式。您可以看到使用排序键。
'set worksheet to work on as active (selected sheet)
Set emailWS = ThisWorkbook.ActiveSheet
startRow = 2 ' starting row
nameCol = 1 'col of names, can also do nameCol = emailWS.Range("A1").Column
deptCol = 3 'col of depts, can also do deptCol = emailWS.Range("A3").Column
'** Advantage of the optional way is if you have many columns and you don't want to count them
'find the last row with a name in it from the name column
lastRow = emailWS.Cells(emailWS.Rows.Count, nameCol).End(xlUp).Row
'sort the data first before going through the email process using Range sort and a key
'assumes these are the only columns 1 (nameCol) thru 3 (deptCol) to sort
'assumes you are sorting based on col 1 (nameCol)
emailWS.Range(Cells(startRow, nameCol), Cells(lastRow, deptCol)).Sort _
key1:=emailWS.Range(Cells(startRow, nameCol), Cells(lastRow, nameCol))
干杯, WWC
答案 2 :(得分:0)
如果要对列A进行排序,则需要将其包含在myRange中。目前你有
ws_catalogue.Sort.SetRange = myRange
myRange
从B1到数据底部(不包括A列)。
此外,如果您想订购A栏,则需要添加类似
的内容 .SortFields.Add _
Key:=myRange.Offset(0,0) _
SortOn:=0, _
Order:=1, _
DataOption:=0