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

相关推荐
开开心心就好15 小时前
键盘改键工具免安装,自定义键位屏蔽误触
java·网络·windows·随机森林·计算机外设·电脑·excel
fqbqrr20 小时前
2601Mfc,自动化excel
自动化·excel·mfc
tlwlmy20 小时前
python excel图片批量导出
开发语言·python·excel
TracyDemo2 天前
excel 透视图怎么进行删除透视图
excel
骆驼爱记录2 天前
Excel邮件合并嵌入图片技巧
自动化·word·excel·wps·新人首发
avi91112 天前
Unity Data Excel读取方法+踩坑记;和WPS Excel的一些命令
unity·游戏引擎·excel·wps·data
梦幻通灵2 天前
Excel多个sheet合并透视表实现方案【持续更新】
excel
开开心心就好2 天前
键盘映射工具改键位,绿色版设置后重启生效
网络·windows·tcp/ip·pdf·计算机外设·电脑·excel
恬淡如雪2 天前
Excel接口测试自动化实战
爬虫·python·excel
速易达网络2 天前
linux命令大全
linux·运维·excel