宇宙尽头是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列,可以按需改

相关推荐
less_1213814 小时前
鸿蒙 WPS Open SDK 二开复盘:从注册、打开到回传的完整能力链
服务器·sdk·harmonyos·wps·鸿蒙开发
Data-Miner18 小时前
一个省大模型积分的 Excel 数据处理方案:脱敏生成逻辑,脚本复用把 token 降到 0
人工智能·windows·excel
Logic1011 天前
表格智能记账3 销售与库存智能联动,自动标红库存预警
excel·条件格式·excel技巧·vlookup·sumif
vipers_2 天前
WPS如何打开:选中单元格,导航栏显示内容
wps
PythonFun2 天前
如何生成Python50天系列学习互动网页课件
python·wps
技术小甜甜2 天前
【Copilot】能选 Claude 和 GPT-6 了,Excel 分析该交给谁
gpt·excel·copilot·claude·microsoft365·gpt-6
brian00313 天前
word插件,像wps一样双击放大图片
word·wps·双击放大图片
小白今天也很酷3 天前
MCXN947更新板载DAP_Link固件的方法
c语言·python·单片机·ubuntu·excel·1024程序员节
ratel️4 天前
小墨析 · Office Agent 日常办公不烧心
python·langchain·word·excel·fastapi·js·ppt
Logic1014 天前
没有TextSplit函数的老版本也能拆分单元格的内容
excel·拆分数据·excel技巧·数据拆分·拆分单元格·textsplit·旧版excel