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

相关推荐
LAM LAB5 天前
【VBA】Excel指定单元格范围内字体设置样式,处理导出课表单元格
excel·vba
在这habit之下5 天前
Keepalived学习总结
excel
Youngchatgpt5 天前
如何在 Excel 中使用 ChatGPT:自动化任务和编写公式
人工智能·chatgpt·自动化·excel
开开心心就好5 天前
安卓开源应用,超时提醒紧急人护独居安全
windows·决策树·计算机视觉·pdf·计算机外设·excel·动态规划
D_C_tyu5 天前
Vue3 + Element Plus | el-table 多级表头表格导出 Excel(含合并单元格、单元格居中)第二版
vue.js·elementui·excel
骆驼爱记录5 天前
WPS页码设置:第X页共Y-1页
自动化·word·excel·wps·新人首发
Cxiaomu6 天前
Python 文件解析: Excel / Word / PDF 的解析、处理、预览与下载
python·word·excel
2501_930707786 天前
如何使用C#代码从 PDF 中提取表格并另存为Excel文件
pdf·excel
pacong6 天前
B生所学EXCEL
人工智能·excel