我想要實現的是在包含列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. 插入空格,如果條件滿足
- 2. 如果條件滿足,則插入
- 3. VBA宏如果表值滿足條件
- 4. 如果在「表」中滿足條件,插入「表1」2 SQL
- 5. Excel VBA中添加新的行如果條件滿足
- 6. 插件如果條件滿足
- 7. PHP的MySQL更新和插入,如果條件滿足
- 8. 如果在另一個表中滿足條件,MySQL插入
- 9. Matlab-如果條件滿足
- 10. 如果滿足條件
- 11. SQL如果條件滿足
- 12. VBA代碼如果條件不滿足,Stlll進入循環
- 13. 打印行,如果條件滿足,R
- 14. Excel VBA如果在遇到不滿足條件時執行
- 15. 在VBA刪除行,如果滿足一定條件
- 16. 插入MySQL測試,如果條件滿足則取消
- 17. 如果滿足條件,則選擇並檢查並插入
- 18. 如果滿足條件,如何在MySQL中選擇一行,如果條件不滿足,則選擇另一行?
- 19. VBA轉置循環並在滿足條件時開始新行
- 20. 如果任何元件滿足條件
- 21. 加入不滿足條件
- 22. SQL結果表滿足兩個條件
- 23. 如何終止,如果條件滿足
- 24. VBA - 如果條件滿足,然後把公式另一列
- 25. 滿足條件
- 26. 插入幾個行以滿足約束
- 27. 滿足條件的行數
- 28. 如果條件不滿足替代值從那裏條件滿足
- 29. 如果滿足條件,則替換字
- 30. 的Python如果條件滿足,打印「...」
你嘗試過自己以後做呢?你可以在這裏尋找幫助: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