根据单元格值插入行 [英] Insert row based on cell value
本文介绍了根据单元格值插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是宏Excel函数的新手,当特定列的单元格值发生变化时,我试图插入一行.例如,
I am new to macro Excel functions and I am trying to insert a row when there is a change in the cell value of a particular column. For example,
row_no B
1 p
2 p
3 p
4 q
5 q
6 q
7 q
由于第1列中的值已更改,因此应在第3行插入一行.你有什么想法?
A row should be inserted at row 3 as the value in column 1 has changed. Do you have any ideas?
现在,这是我的代码.
Sub MySub()
Do While B1 <> B2
CurrentSheet.Range("a1:i1").EntireRow.Insert
Loop
End Sub
它仍然无法正常工作,你们所有人都知道为什么吗?
It is still not working, do all of you have any idea why?
推荐答案
尝试以下代码:
Sub Demo()
Dim ws As Worksheet
Dim lastRow As Long, i As Long
Set ws = ThisWorkbook.Sheets("Sheet1") 'set you data sheet here
lastRow = Cells(Rows.Count, "A").End(xlUp).Row 'get the last row in column A
For i = lastRow To 2 Step -1 'loop from last row to row 2
If ws.Range("A" & i) <> ws.Range("A" & i - 1) Then 'compare value if not same
ws.Range("A" & i).EntireRow.Insert 'if value are not same insert row
End If
Next i
End Sub
这篇关于根据单元格值插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文