excel - 按值排序
问题描述
我有这个代码:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Column = 1 Then
Dim lastRow As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Range("A5:J33" & lastRow).Sort key1:=Range("A5:A33" & lastRow), order1:=x1Ascending, Header:=xlNo
End If
End Sub
我要排序的范围是 A5:J33,我希望它的排序方式是输入后 A5:A33 中的数据
任何帮助,将不胜感激
解决方案
您在 A 列中获得了 lastRow lastRow = Cells(Rows.Count, 1).End(xlUp).Row
,但您没有正确实现结果:
Range("A5:J33" & lastRow)
并且Range("A5:A33" & lastRow)
应该是Range("A5:J" & lastRow)
和Range("A5:A" & lastRow)
。
试试下面的修改代码:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Column = 1 Then
Dim lastRow As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=Range("A1:A" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveSheet.Sort
.SetRange Range("A1:J" & lastRow)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End If
End Sub
推荐阅读
- json - React-Select Multi Searchable:不允许我通过分隔的字母或单词片段进行搜索
- mysql - InnoDB 是否会锁定整个表以进行使用组合键的一部分的删除?
- javascript - 如何在 Vue 中使过滤列表更加动态
- android - 通过 adb-shell 命令“fastboot 擦除缓存”从手机上的所有应用程序中擦除缓存
- angular - Angular Firebase 2 - 从数据库中读取数据一次
- spreadsheet - 使用 Python 2.65 写入电子表格
- apache - Apache 服务器未在 Xampp 便携版上启动
- php - 迭代数据并将csv文件放入php中的问题
- go - 获取调用函数的名称和包
- twilio - 如何在客户端之间转移 twilio 入站呼叫