功能描述
- 检测第N列的数据从某行开始变化后,在变化的位置插入3行空白行
- 再次检测M列,如果之前已经插入过空白行则不再插入
代码实现
Attribute Module_Name = "1"
function Macro1()
{
var sheet = Application.ActiveSheet;
var columnA = sheet.Range("A:A");
var lastRowA = columnA.Cells(columnA.Rows.Count).End(-4162).Row;
var columnB = sheet.Range("B:B");
var lastRowB = columnB.Cells(columnA.Rows.Count).End(-4162).Row;
var startRow = 1;
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
}
}
}