我想要实现的是在包含列A文本“rich”的特定行之后插入新行的数量,并且如果同一行中的列B包含的值小于10在该行之后插入2行。但如果同一行中的列B包含的值较高,则在该行后面插入1行。我不是编写循环代码的最佳人选。我会感谢任何帮助。如果满足2个条件,VBA插入新行
-3
A
回答
1
我管理了一段时间:)
Sub macro1()
Range("A1").Select
' remember the last row that contains a value
Dim LastRow As Integer
Dim CurrentRow As Integer
LastRow = Range("A1").End(xlDown).Row
CurrentRow = 1
' keep on incrementing the current row until we are
' past the last row
Do While CurrentRow <= LastRow
' if the desired string is found, insert a row above
' the current row
' That also means, our last row is going to be one more
' row down
' And that also means, we must double-increment our current
' row
If Range("A" & CurrentRow).Value = "rich" And Range("B" & CurrentRow).Value > 10 Then
Range("A" & CurrentRow + 1).EntireRow.Insert xlIp
Range("A" & CurrentRow + 1).EntireRow.Insert xlIp
LastRow = LastRow + 2
CurrentRow = CurrentRow + 1
ElseIf Range("A" & CurrentRow).Value = "rich" And Range("B" & CurrentRow) < 10 Then
Range("A" & CurrentRow + 1).EntireRow.Insert xlUp
LastRow = LastRow + 1
CurrentRow = CurrentRow + 1
End If
' put the pointer to the next row we want to evaluate
CurrentRow = CurrentRow + 1
Loop
End Sub
+1
你在第一个'if'不应该是'CurrentRow = CurrentRow + 2'吗? –
+1
您也可以将'Range(“A”&CurrentRow + 1).EntireRow.Insert xlIp'改为'Rows(CurrentRow + 1&“:”&CurrentRow + 2)。插入' –
+0
嗯可能是的,但代码以某种方式工作:D – eurano
相关问题
- 1. Excel VBA中添加新的行如果条件满足
- 2. PHP的MySQL更新和插入,如果条件满足
- 3. 如果在另一个表中满足条件,MySQL插入
- 4. Matlab-如果条件满足
- 5. VBA代码如果条件不满足,Stlll进入循环
- 6. 打印行,如果条件满足,R
- 7. Excel VBA如果在遇到不满足条件时执行
- 8. 插入MySQL测试,如果条件满足则取消
- 9. 如果满足条件,则选择并检查并插入
- 10. 如果满足条件,则添加新行然后求和列
- 11. 加入不满足条件
- 12. 如何终止,如果条件满足
- 13. VBA - 如果条件满足,然后把公式另一列
- 14. 插入几个行以满足约束
- 15. 满足条件的行数
- 16. 如果条件不满足替代值从那里条件满足
- 17. 提取值,如果条件满足
- 18. 比较时间,如果条件满足
- 19. 如果满足条件,则替换字
- 20. Map对象,如果条件满足
- 21. 序言 - 添加如果条件满足
- 22. 做,如果满足第一条件
- 23. 如果条件满足,选择参数
- 24. 如果条件满足,然后运行多个ifs
- 25. Scrapy:停止爬行一个域,如果条件满足
- 26. MySQL的:如果条件满足串联2列
- 27. 如果2 3的条件都满足(W/O ||“OR”)
- 28. MySQL的JOIN如果任一2代表的满足条件
- 29. Python - 如何检查是否满足几个条件之一,然后特别是如果满足每个条件?
- 30. 满足两个条件值后插入内容
你尝试过自己以后做呢?你可以在这里寻找帮助:http://stackoverflow.com/questions/1463236/loop-through-each-row-of-a-range-in-excel 和在这里: http://stackoverflow.com/questions/15816883/excel-vba-inserted-blank-row-and-shifting-cells – Wouter