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

相关推荐
hmywillstronger1 小时前
【Python】从SAP2000 XML截面库提取数据到Excel
xml·python·excel
抹茶咖啡2 小时前
IT运维的365天--045 WPS突然就不能正常打开Excel文件了
excel·it运维·wps
专注VB编程开发20年2 小时前
在 Python 中使用 comtypes 时,大小写通常必须保持精确
python·excel
E_ICEBLUE1 天前
如何提取 Word 文档中的表格并导出为 Excel(Python 教程)
python·word·excel
2501_930707781 天前
使用C#代码在 Excel 中创建雷达图
信息可视化·excel
2501_907136821 天前
发票批量打印工具,仿写WPS发票打印工具
软件需求·wps
fengyehongWorld1 天前
Excel Excel2024版本之后,行与列相关的函数
excel
小贺儿开发1 天前
Unity3D 年会抽奖工具(附体验链接)
数据库·unity·excel·人机交互·工具·抽奖·互动
Eiceblue1 天前
锁定单元格 :C# 控制 Excel 单元格编辑权限
开发语言·c#·excel