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

相关推荐
星沙丘秋1 天前
Kettle导入Excel文件进数据库时,数值发生错误的一种原因
excel
Tomorrow'sThinker1 天前
✍️ Python 批量设置 Word 文档多级字体样式(标题/正文/名称/小节)
python·自动化·word·excel
大虫小呓1 天前
50个Python处理Excel示例代码,覆盖95%日常使用场景-全网最全
python·excel
禁默1 天前
Linux Vim 编辑器详解:从入门到进阶(含图示+插件推荐)
linux·vim·excel
Tomorrow'sThinker2 天前
[特殊字符] Python 批量生成词云:读取词频 Excel + 自定义背景 + Excel to.png 流程解析
python·excel
UrbanJazzerati2 天前
Excel 使用中的“坑”:拆分与合并列的陷阱及解决方案
excel
KeThink3 天前
国民经济行业分类 GB/T 4754—2017 (PDF和exce版本)
pdf·excel
_oP_i3 天前
Excel 的多线程特性
excel
V1ncent Chen3 天前
Excel基础:数据查看
excel
谁他个天昏地暗3 天前
Java 实现 Excel 文件对比与数据填充
java·开发语言·excel