跳到主要内容

如何在Excel中按值自动对列进行排序?

例如,您有一个如下图所示的采购表。 现在您希望在此列中输入新数字/价格时自动对价格列进行排序,您该如何解决? 在这里,我介绍一个 VBA 宏来帮助您在 Excel 中按值自动对特定列进行排序。

使用VBA按值自动对列进行排序


使用VBA按值自动对列进行排序

一旦您在Excel中输入新数据或更改列中的值,此VBA宏就会自动对特定列中的所有数据进行排序。

1。 右键单击当前工作表名称 工作表标签栏,然后单击 查看代码 从右键单击菜单中。

2。 在打开的Microsoft Visual Basic for Application对话框中,将以下VBA宏代码粘贴到打开的窗口中。

VBA:Excel中的自动排序列

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("B:B")) Is Nothing Then
Range("B1").Sort Key1:=Range("B2"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub

备注:
1)在上面的vba代码中, B:B 意味着它将自动对B列进行排序, B1 是 B 列中的第一个单元格, B2 是 B 列中的第二个单元格,您可以根据需要更改它们。
2) 片段 标头:=xl是 在第 5 行告诉 Excel 您将排序的范围有一个标题,以便在排序时不包括该范围的第一行。 如果没有标题,请将其更改为 标题:=xlNo; 和改变 键1:=范围(“B2”) 在第 4 行 键1:=范围(“B1”).

3。 然后返回工作表,当您在“价格”列中输入新数字或修改任何现有价格时,“价格”列将自动按升序排序。

备注:当您在价格列中输入新数字时,您必须在原始数字下方的第一个空白单元格中输入数字。 如果新输入的数字与原数字之间有空白单元格,以及原数字之间有空白单元格,则该列不会自动排序。


演示:使用Excel中的VBA自动按值对列进行排序


Kutools for Excel:超过 300 个方便的工具触手可及! 立即开始 30 天免费试用,没有任何功能限制。 立即下载!

在Excel中轻松按出现频率排序

Kutools for Excel的 高级排序 该实用程序支持在Excel中按文本长度,姓氏,绝对值,频率等对数据进行快速排序。


广告按频率2排序

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (37)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
such a good information thanks a lot
This comment was minimized by the moderator on the site
Maravilhoso! Muito obrigada pela informação, amigo!
Rated 5 out of 5
This comment was minimized by the moderator on the site
I love this, but I'd like to know what I should do if I want it to be multiple different ranges in the same column. When I use the code above, it works for the first table that I have but the bottom two tables don't sort automatically. I tried changing the range, I also duplicated the code and changed the code to match the tables, but nothing is working.

For example:
Range("L8").Sort Key1:=Range("L37"),Range("L41").Sort Key1:=Range("L62") _

Or just duplicating the code like this:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("L:L")) Is Nothing Then
Range("L8").Sort Key1:=Range("L37"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("L:L")) Is Nothing Then
Range("L41").Sort Key1:=Range("L62"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("L:L")) Is Nothing Then
Range("L66").Sort Key1:=Range("L100"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom

But then it will tell me that "Ambiguos name detected: Worksheet_Change" but it won't do that if I only have the code one. Would anyone be able to help me out?
Thank you!
This comment was minimized by the moderator on the site
Hi there,

You can use the vba below:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    If Not Intersect(Target, Range("K:K")) Is Nothing Then
        Range("K32:K36").Sort Key1:=Range("K32"), _
        Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom
        
        Range("K38:K42").Sort Key1:=Range("K38"), _
        Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom        

        Range("K44:K46").Sort Key1:=Range("K44"), _
        Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom
    End If
End Sub


Amanda
This comment was minimized by the moderator on the site
01. I have sorted on Name data in Excel Worksheet as Sheet1.
02. I want the Names that are repeated in New Worksheet as Sheet2.
This comment was minimized by the moderator on the site
Hallo
Ich habe folgenden Code, aber die Sortierung klappt leider nicht.
evtl nur eine kleine Anpassung, aber ich verzweifle hier seit Tagen.
Danke im voraus.


Sub Eindeutige_Daten()

Dim rng As Range
Dim InputRng As Range, OutRng As Range

Set dt = CreateObject("Scripting.Dictionary")

xTitleId = "Eindeutige Daten"

Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)

For Each rng In InputRng
If rng.Value <> "" Then
dt(rng.Value) = ""
End If
Next

OutRng.Range("A1").Resize(dt.Count) = Application.WorksheetFunction.Transpose(dt.Keys)

'** Zelladresse in Spalten- und Zeilenangabe trennen
Dim wert() As String
wert = Split(OutRng.Address, "$")

letztezeile = ActiveSheet.Cells(1048576, wert(1)).End(xlUp).Row

With ActiveWorksheet.Sort
.SetRange Range(OutRng & ":" & wert(1) & letztezeile)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Calculate

End Sub
This comment was minimized by the moderator on the site
Hi there,

Please debug the below snipt of your code and see if there is a problem.
With ActiveWorksheet.Sort
.SetRange Range(OutRng & ":" & wert(1) & letztezeile)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Remember to check the values you entered.
If there is still questions, please don't hesitate to ask me.

Amanda
This comment was minimized by the moderator on the site
Love it. Works for me.

But when I repeat the exact same steps at another Excel file, and enter a number in the colomn for it to autosort, Excel closes.
This comment was minimized by the moderator on the site
Hi,
This is extremely useful.
Is there a way to expand the formula to cover multiple columns? For example, to sort data based on values on first, column B, and then column C?I would really appreciate any solutions!
This comment was minimized by the moderator on the site
Amazing thanks!!!
This comment was minimized by the moderator on the site
So this seems to work when the data is manually entered but doesn't work when it is a table that repopulates from another file....is there any way to do that?
This comment was minimized by the moderator on the site
Hi, it arrange the entire row but I have some link on cells into specific folder which is left behind after sort.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations