我正在尝试在excel中构建一个宏,它将采用这样的大量数据:
D 1 2 3 4 5
D 1 2 3 9 5
D 1 2 3 4 5
当第4列中的值不同时,处理它以插入行。我还想使用静态值或公式同时填充此行。
理想情况下,按照上表我会得到:
D 1 2 3 4 5
H A B C D E <- This row got added as there was a change in column D
D 1 2 3 9 5
H A B C D E <- This row got added as there was a change in column D
D 1 2 3 4 5
我希望这可以迭代很长的列表。
任何人都可以给我任何指示吗?
感谢您的帮助。
答案 0 :(得分:0)
这样的事情应该有效。我没有测试它但是如果你运行它并使用F8进行迭代它应该很容易调试,如果它没有完全按预期工作。
Dim i as integer
for i = 2 to cells(1,1).end(xldown).row 'itereate thru all rows
if cells(i,4).value <> cells(i-1,4).value then 'compare the cells in col 4 with previous row
rows(i).entirerow.insert 'insert a row if the values don't match
cells(i,1) = "A"
cells(i,2) = "B"
cells(i,3) = "C"
cells(i,4) = "D"
cells(i,5) = "E"
i = i + 1 'since we inserted a row we have to make i bigger to go down
end if
next i