宇宙尽头是WPS之——【Excel】一个自动重新排序的宏

1. 目的

你是否在做一个表格排序,但只能知道某几个行之间的相对顺序,而可能排着排着发现后面还有顺序更靠前的项,而不得不将排好的序号重新+1+1......

所以你需要一个宏,它可以知道你输入了一个已经存在的序号,并以那个序号为准,自动修改其他序号

举两个例子:

原始 修改 修改后
3 3
3 4
7 7
2 2
1 1
原始 修改 修改后
3 4
4 5
7 2 2
2 3
1 1

2. 宏代码

复制代码
Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Target.Worksheet.Range("A:A")) Is Nothing Then
        Dim ws As Worksheet
        Set ws = Target.Worksheet
        Dim userInputRow As Integer
        userInputRow = Target.Row
        Dim userInputValue As Integer
        userInputValue = Target.Value

        Dim i As Integer
        Dim lastRow As Integer
        lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

        For i = 1 To lastRow
            If i <> userInputRow Then
                If ws.Cells(i, 1).Value = userInputValue Then
                    ws.Cells(i, 1).Value = userInputValue + 1
                End If
            End If
        Next i
    End If
End Sub

宏怎么用就不多说了,这里是把排序列定为A列,可以按需改

相关推荐
斯特凡今天也很帅29 分钟前
Excel在保留下拉选项的基础上,通过输入四级目录数据,在一级目录、二级目录、三级目录、五级目录的显示
excel
全栈开发圈1 天前
新书速览|Excel+DeepSeek会计与财务高效办公
语言模型·excel
HealthScience1 天前
WPS打开文件很慢
wps
ew452181 天前
【java】基于hutool实现.Excel导出任意多级自定义表头数据
java·开发语言·excel
SunnyDays10111 天前
使用 Python 在 Excel 中应用数据验证:详细指南
python·excel·数据验证
开开心心就好1 天前
模拟真人手写软件,支持随机调节
运维·服务器·windows·gitee·pdf·开源·excel
SunnyDays10112 天前
如何使用 Python 操作 Excel 图片:插入、提取与压缩
python·excel·提取图片·压缩图片·插入图片到excel·删除图片
骆驼爱记录2 天前
Word一键批量添加图章
自动化·word·excel·wps·新人首发
TMT星球2 天前
金山办公2025年财报:营收净利双增,WPS 365业务同比增长64.93%
人工智能·wps
PythonFun2 天前
WPS AI助力字母序号高效填充:四种快捷方法
人工智能·wps