提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

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

例如,您将显示一个购买表,如左侧的屏幕截图所示。 现在,当您在此栏中输入新号码/价格时,您希望自动对价格列进行排序,那么您如何解决? 这里我介绍一个VBA宏来帮助你在Excel中按值自动排序一个特定的列。

使用VBA自动按值排序列

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

Kutools为Excel的 高级排序 实用程序支持通过Excel中的文本长度,姓氏,绝对值,频率等快速排序数据。 全功能免费试用60天!
广告按频率2排序

Office选项卡在Office中启用选项卡式编辑和浏览,使您的工作更轻松......
Kutools for Excel - 最佳办公生产力工具将解决您的大部分Excel问题
  • 重用任何东西: 将最常用或最复杂的公式,图表和其他任何内容添加到您的收藏夹中,并在将来快速重复使用它们。
  • 超过20文本功能: 从文本字符串中提取数字; 提取或删除部分文本; 将数字和货币转换为英语单词...
  • 合并工具:多个工作簿和表格合二为一; 合并多个单元格/行/列而不丢失数据; 合并重复行和总和...
  • 拆分工具:根据价值将数据拆分为多个表格; 一个工作簿到多个Excel,PDF或CSV文件; 一列到多列......
  • 粘贴跳过 隐藏/过滤行; 数和总和 按背景颜色; 创建邮件列表和 通过Cell的价值发送电子邮件...
  • 超级过滤器: 创建高级过滤方案并应用于任何工作表; 排序 按周,日,频率等; 筛选 通过大胆,公式,评论......
  • 超过300强大的功能; 适用于Office 2007-2019和365; 支持所有语言; 在公司轻松部署; 全功能60天免费试用。

箭头蓝色右泡使用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

注意:在上面的vba代码中,B:B表示将自动对列B进行分类,B1是B列中的第一个单元格,B2是B列中的第二个单元格,您可以根据需要更改它们。

3。 然后返回工作表,当您在价格栏中输入新号码或修改任何现有价格时,价格栏会自动按升序排序。

注意:在价格栏中输入新号码时,必须在原始号码下面的第一个空白单元格中输入号码。 如果新输入的号码与原始号码之间存在空白单元格,以及原始号码之间的空白单元格,则此列不会自动排序。


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

在这个视频中, Kutools Kutools Plus 选项卡添加 Kutools for Excel。 如果需要,请点击 60天免费试用,不受限制!

Kutools for Excel - 最佳办公生产力工具提高80%的生产力

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能60天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Paula · 4 months ago
    May I ask if there is a way to use this VBA code if there is a formula in the column that you want to be the main sort.

    i.e. if A2 = the sum of f2 + g2+ h2 and column A needs to be sorted on the total sum? (I can't get VBA to work if formula in the cells)

    Any assistance would be kindly appreciated as hope to use it for ranking races when the winner is the one with the total of three racing events.

    Thank You
  • To post as a guest, your comment is unpublished.
    Bekir · 5 months ago
    Hi,

    How can I make this macro start after I fill all data to the specific column?
    I want the listing macro start after I fill empty boxes. Because when I use this, it changes the place suddenly.
    I need to start this macro after I filled the last data of that column (20 datas to be exact)
  • To post as a guest, your comment is unpublished.
    Rhea · 1 years ago
    Hello, I am an intermediate Excel user. I am wondering if someone could help direct me for what I am trying to accomplish. Here is what I am trying to do: Auto-Sort with Custom Order.

    A little about my spreadsheets: I have one sheet (named: Sheet1) that lists 16 columns (A through P) and currently 19 rows (this number will increase as more data is entered). In the last column (column P) all cells have a drop down menu of options (located and pulled from sheet two (named "BackEnd"). The drop down selections are color-coded (using conditional formatting rules).

    What I am hoping to do: As a user enters data in a new row, as soon as they are finished selecting an option from the drop down menu in column P, Excel will auto-sort the new row to a specific order. I have been able to create VBA code for auto-sorting in ascending and descending order, but I have not been able to create a code for auto-sorting the data in a specific order. The order I would like to have the data sorted into is:

    Unresponsive
    Not Interested
    Interested
    Pre-Screened Not Qualified:
    Pre-Screened Qualified
    Application Pending
    Application Approved
    Initial Assessment:
    Scope of Work:
    Home Repairs in Progress
    On Hold:
    Complete
    Other:

    Is this possible? Any help is appreciated! Thank you!
  • To post as a guest, your comment is unpublished.
    Stephen · 1 years ago
    While this is a fantastic code, I would like to know if anyone could help me get it go from ascending to descending? In other words, I'm looking for it to be the largest number on top with the lowest number on the bottom
    • To post as a guest, your comment is unpublished.
      Xander · 1 years ago
      Change (Order1:=xlAscending, Header:=xlYes, ) To (Order1:=xlDescending, Header:=xlYes, )
  • To post as a guest, your comment is unpublished.
    SHABEERALI · 1 years ago
    Any one know how to set set an auto serial no. for an excel work sheet. The number should excludes all heading and sub headings, and should auto re arrange after adding or deleting a row.
    • To post as a guest, your comment is unpublished.
      Tang Kelly · 1 years ago
      Hi,
      Kutools for Excel provides an Insert Sequence Number feature, which can insert unique and consecutive values no matter how many times you insert.
      For example, the first time you insert 1,2,3,4;
      The second time this feature will insert 5,6,7,8,9,10
      The third time this feature will insert 11,12,…
  • To post as a guest, your comment is unpublished.
    Sushant · 2 years ago
    Thanks for code and logic. The above logic is not working when we are looking at bigger range of cells like Intersect(Target, Range("B:B,C:C,D:D")). I want if there is change in any cell in the Column (B,C or D), resorting needs to be done.
  • To post as a guest, your comment is unpublished.
    Tarcisio · 2 years ago
    THANK YOU!!!
  • To post as a guest, your comment is unpublished.
    Logan · 2 years ago
    Copied the code exactly into my sheet in VBA. But nothing is happening am I missing something? Did exactly what you did.
  • To post as a guest, your comment is unpublished.
    Rezal · 2 years ago
    First row is excluded for the sorting
    • To post as a guest, your comment is unpublished.
      Red Code · 1 years ago
      I had the same problem and I found out why. Go to line 5, (Order1:=xlAscending, Header:=xlYes, _). The script is telling Excel the first cell is a header, and therefore it will ignore it when the sorting takes place. If you want to include the first row, change it to the following line:


      Order1:=xlAscending, Header:=xlNo, _
  • To post as a guest, your comment is unpublished.
    nikos · 2 years ago
    Hello. Excellent trick, thank you very much. In my file, I have important info which I need to see in cells A1 and A2. Therefore, I have stabilized all cells and my values start from A3 to A500. When I execute this vba, my info in cell A2 goes in cell 501. How can I make it start ranging form cell A3? Thank you in advance!
  • To post as a guest, your comment is unpublished.
    Brandon · 2 years ago
    Say I need to auto sort column "C" and column "D". How would I modify the code to sort both columns?
  • To post as a guest, your comment is unpublished.
    SK · 2 years ago
    This is GREAT. This was of great help.
  • To post as a guest, your comment is unpublished.
    Dean Marshall · 2 years ago
    I am looking for the same automatic sorting when entering data for my excel chart. Finding it hard to understand the details of what you have entered here as example. I would really appreciate it if someone here who did the example above could help me sort my sheet out so it did the same. I have a F1 chart that shows winners, and points, and would like it so that when the points are entered next to the drivers name for a specific race for the total on the right hand side of the sheet to be in numerical order, WITHOUT MIXING THE RESULT against other drivers. It seems that I can not place on this message an attachment of my chart so if you are able to alter my chart for me so this will happen I would like to hear from you. I can send it via email to you. If you can help with this then please email me at lordmarshall2013@gmail.com and in the SUBJECT line please write F1 Excel help and will reply with the chart attached for you. Thanks in advance for any help you can provide.
  • To post as a guest, your comment is unpublished.
    Alicia · 3 years ago
    Beautiful, beautiful, beautiful, thank you SO much. Would have never been able to figure this out on my own and I really appreciate you sharing this!!!