PDA

View Full Version : Copy the last category and insert custom text



chrisade
01-21-2022, 02:00 AM
Hello guys, this code will copy twice the entire cell from last row for each category(A,B,C,D...). Now how to insert custom text for column E and P (Green column)? So the 1st copy and the 2nd copy will have different value and it will be nice if I can use inputbox to determine those values. So it will ask:


How many row
Text 1 for E
Text 2 for E
Text 1 for P
Text 2 for P


any help would be greatly appreciated

29343



Sub Zeile_einfügen()
Dim sh As Worksheet, lastRow As Long, rngOO As Range, Fnd1 As Range
Dim i As Long, Count As Long, arr, dict As Object

Count = Application.InputBox(Prompt:="How many row?", Default:=2)
Txt1E= Application.InputBox(Prompt:="1st Item Number?")
Txt2E= Application.InputBox(Prompt:="2nd Item Number?")
Txt1P= Application.InputBox(Prompt:="1st Kommentar_2?")
Txt2P= Application.InputBox(Prompt:="2nd Kommentar_2?")

Set sh = ActiveSheet
lastRow = sh.Range("O" & sh.Rows.Count).End(xlUp).Row
Set rngOO = sh.Range("O3:O" & lastRow)
arr = rngOO.Value 'place the row in an array for faster iteration

Set dict = CreateObject("Scripting.Dictionary")
'Extract the unique categories:
For i = 1 To UBound(arr)
If arr(i, 1) <> "" Then dict(arr(i, 1)) = Empty
Next i

'finding the last unique categories and do copy its row of Count times:
For i = 0 To dict.Count - 1
Set Fnd1 = rngOO.Find(dict.Keys()(i), , , xlWhole, xlByRows, xlPrevious, False, , False)
Fnd1.EntireRow.Copy
sh.Range(Fnd1.Offset(1, 0), Fnd1.Offset(Count, 0)).EntireRow.Insert Shift:=xlDown
Next i
MsgBox (Count & " Row/s are added")
End Sub


Link to the other post:
https://www.excelforum.com/excel-programming-vba-macros/1369006-copy-the-last-category-and-insert-custom-text.html#post5623069

georgiboy
01-21-2022, 02:42 AM
This has been cross posted:

https://www.excelforum.com/excel-programming-vba-macros/1369006-copy-the-last-category-and-insert-custom-text.html#post5623094

There are rules on cross posting, from my understanding it is allowed however you should provide a link to the other post.

chrisade
01-21-2022, 04:28 AM
Thank you so much for your info