PDA

View Full Version : Insert cell at specific rows by string



YasserKhalil
03-22-2020, 11:53 AM
Hello everyone


I have a string say s="11,19" in a code
How can I insert cell down at those rows 11 and 19 in column G for example.. and insert specific text after insert .. and also at the first empty row after last cell in the same column?

26194

大灰狼1976
03-23-2020, 09:29 PM
Hi YasserKhalil

Sub test()
Dim arr, s$, i&, r&, r1&
arr = Array(11, 19)
s = "Tot."
r1 = 1
For i = 0 To UBound(arr)
r = arr(i) + 1
Cells(r, "g").Resize(, 2).Insert Shift:=xlDown
Cells(r, "g").Resize(, 2) = Array(s, "=sum(h" & r1 & ":h" & r - 1 & ")")
r1 = r + 1
Next i
r = Cells(Rows.Count, "g").End(3).Row
Cells(r + 1, "g").Resize(, 2) = Array(s, "=sum(h" & r1 & ":h" & r & ")")
End Sub

YasserKhalil
03-24-2020, 12:58 AM
Thanks a lot for your help. It seems no way of looping. Is there a way to do such stuff without looping?

大灰狼1976
03-24-2020, 01:37 AM
This loop is not in cells, it's in conditions array.
There's no way not to loop, unless you have only one condition.
If my code doesn't work, please refer to the attachment.

YasserKhalil
03-24-2020, 01:57 AM
It is working well. Thank you very much for your great help.