直接上代码
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
}
}
}
使用说明
- 修改代码的第6和第8行,这是监测变化的行
- 如需修改插入的行的数量,需要修改for循环中的i=i+n的部分和lastRowB = lastRowB +3、lastRowA = lastRowA +3的部分
评论 (0)