WPS宏-实现根据内容变化插入指定数量的单元格
侧边栏壁纸
  • 累计撰写 62 篇文章
  • 累计收到 47 条评论

WPS宏-实现根据内容变化插入指定数量的单元格

Skycyan
2023-06-09 / 0 评论 / 130 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年06月09日,已超过714天没有更新,若内容或图片失效,请留言反馈。

直接上代码

function Macro1() 
{
    var sheet = Application.ActiveSheet; // 获取当前活动工作表对象
    var columnA = sheet.Range("A:A"); // 获取A列范围
    var lastRowA = columnA.Cells(columnA.Rows.Count).End(-4162).Row; // 获取A列的最后一行B
    var columnB = sheet.Range("B:B"); // 获取A列范围
    var lastRowB = columnB.Cells(columnA.Rows.Count).End(-4162).Row; // 获取B列的最后一行

    var startRow = 3; // 从第n行开始判断

    for (var i = startRow; i <= lastRowA; i++) {
        var currentValue = columnA.Cells(i).Value(); // 当前行的值
        var nextValue = columnA.Cells(i + 1).Value(); // 下一行的值

        if (currentValue != nextValue && nextValue != null) {
            sheet.Rows(i + 1 + ":" + (i + 3)).Insert(-4162); // 在不一致的位置插入三行空白行
            i = i +3
            lastRowA = lastRowA +3
            lastRowB = lastRowB +3
        }
        else if(currentValue == null|| currentValue == ""){
            Console.log("空值")    
            i = i + 3
            continue
            
            }
    }
    
    for (var i = startRow; i <= lastRowB; i++) {
        var currentValue = columnB.Cells(i).Value(); // 当前行的值
        var nextValue = columnB.Cells(i + 1).Value(); // 下一行的值
        Console.log(currentValue)

        if (currentValue != nextValue && nextValue!= null) {
            sheet.Rows(i + 1 + ":" + (i + 3)).Insert(-4162); // 在不一致的位置插入三行空白行
            i = i +3
            lastRowB = lastRowB +3
            lastRowA = lastRowA +3
    }
        else if(currentValue == null || nextValue == null){
            Console.log("空值")
            i = i + 3
            continue
        }

    }
}

使用说明

  1. 修改代码的第6和第8行,这是监测变化的行
  2. 如需修改插入的行的数量,需要修改for循环中的i=i+n的部分和lastRowB = lastRowB +3、lastRowA = lastRowA +3的部分
0

评论 (0)

取消