excel双击列标题自动排序的VBA代码
时间:2023-02-24 02:49:46
通过以下的VBA代码,将其放到工作表的双击事件中,就能取到双击工作表的列标题(双击列的名字),当前列自动进行排序。
比如,将代码放在如下的事件中:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
代码如下:
Dim YouRg As Range
If Target.Column <= Me.Cells (1,1).CurrentRegion.Columns.Count And Target.Row = 1 Then
If Target.Column <> mnColumn Then
mnColumn = Target.Column
mnDirection = xlAscending
Else
If mnDirection = xlAscending Then
mnDirection = xlDescending
Else
mnDirection = xlAscending
End If
End If
Set YouRg = Me.Cells(1, 1).CurrentRegion
YouRg .Sort key1:=YouRg .Cells(1, mnColumn), order1:=mnDirection,header:=xlYes
Set YouRg = Nothing
Cancel = True
End If
以上代码是升序排序,你可以改为降序排序。
标签:excel双击列标题自动排序的VBA代码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
word怎样添加字体
2023-06-24 06:45:13
ceb文件怎么打开?ceb文件打开教程
2023-11-06 20:21:15
![](https://img.aspxhome.com/file/2023/8/47958_0s.png)
知识普及,给大家盘点微软Office发展史
2023-10-31 00:47:17
![](https://img.aspxhome.com/file/2023/3/16453_0s.jpg)
在Word2010中默认保存为Word2003DOC文件
2022-01-17 18:46:48
![](https://img.aspxhome.com/file/2023/0/21800_0s.jpg)
如何清除word中的空行?Word清除空行方法介绍
2023-09-30 17:04:30
![](https://img.aspxhome.com/file/2023/6/23186_0s.jpg)
Word怎么制作条形统计图
2023-11-17 21:23:35
![](https://img.aspxhome.com/file/2023/7/17827_0s.jpg)
excel显示第几页怎么设置
2022-06-28 00:52:13
![](https://img.aspxhome.com/file/2023/9/36199_0s.jpg)
Win10开始菜单点击无效怎么办?Win10开始菜单打不开要怎么解决?
2023-09-27 03:34:18
![](https://img.aspxhome.com/file/2023/7/47427_0s.jpg)
Word2016中怎么插入图表
2023-11-12 04:46:18
![](https://img.aspxhome.com/file/2023/8/17738_0s.jpg)
word中插入根号的两种方法
2022-10-27 00:59:27
Word2010文档不能打开问题的解决方法
2023-09-08 01:15:03
Word表格中内容太多不能全部显示该怎么设置?
2023-10-18 21:03:25
![](https://img.aspxhome.com/file/2023/1/28851_0s.jpg)
iPhone 14微信闪退怎么办?iPhone 14微信闪退解决办法
2023-05-29 07:52:28
![](https://img.aspxhome.com/file/2023/6/44776_0s.png)
将常用Word2003文档名称添加到Word工具栏
2023-08-11 04:46:33
![](https://img.aspxhome.com/file/2023/2/21962_0s.jpg)
最实用的word使用技巧 word高手笔记
2023-12-11 05:24:25
![](https://img.aspxhome.com/file/2023/5/32725_0s.jpg)
下载的字体放到fonts之后看word里并没有出现新的字体怎么回事?
2023-11-30 05:30:44
Win10纯净版电脑显示器exit是什么意思?
2023-11-20 01:18:33
![](https://img.aspxhome.com/file/2023/5/50065_0s.png)
Word字体最大只有72吗?
2023-12-08 16:50:21
![](https://img.aspxhome.com/file/2023/9/20229_0s.jpg)
怎样为Word2013文档设置单色背景颜色
2023-06-19 09:17:56
![](https://img.aspxhome.com/file/2023/0/20950_0s.jpg)
怎么在word中删除答案
2023-12-06 15:11:03
![](https://img.aspxhome.com/file/2023/2/19302_0s.jpg)