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

相关推荐
瓶子xf2 小时前
使用Excel制作甘特图
excel·甘特图
战族狼魂6 小时前
Excel 连接阿里云 RDS MySQL
mysql·阿里云·云计算·excel
cypking8 小时前
vue excel转json功能 xlsx
vue.js·json·excel
专注VB编程开发20年8 小时前
C#教程之NPOI读写excel文件XLS,XLSX格式
数据库·microsoft·c#·excel·xlsx·xls
YC运维9 小时前
WEB虚拟主机3种部署方式全解析
excel
Dxy12393102163 天前
Python如何合并两个Excel文件
爬虫·python·excel
wtsolutions4 天前
Batch Conversion Online JSON Files (from URL) to Excel by WTSolutions
json·excel·batch
码尚云标签4 天前
导入Excel打印
excel·excel导入·标签打印软件·打印知识·excel导入打印教程
lilv665 天前
python中用xlrd、xlwt读取和写入Excel中的日期值
开发语言·python·excel
大虫小呓5 天前
14天搞定Excel公式:告别加班,效率翻倍!
excel·excel 公式