WPS实现在某行文字开始变化后插入N行

该代码实现了一个VBA宏,用于检查Excel工作表的A列和B列。从第二行开始,当检测到连续两行数据不一致时,会在变化位置插入三行空白行。宏还处理了空值情况,并更新了最后一行的索引。如果已插入过空白行,宏将跳过后续的插入操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

功能描述

  1. 检测第N列的数据从某行开始变化后,在变化的位置插入3行空白行
  2. 再次检测M列,如果之前已经插入过空白行则不再插入

代码实现

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

    var startRow = 1; // 从第2行开始判断
//	Console.log(lastRow)
    for (var i = startRow; i <= lastRowA; i++) {
        var currentValue = columnA.Cells(i).Value(); // 当前行的值
        var nextValue = columnA.Cells(i + 1).Value(); // 下一行的值
//		Console.log(currentValue)
//		Console.log(nextValue)
        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)
//			Console.log(nextValue)
		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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值