Hii
My code below detects whether a certain number is in row K, if it finds it then it goes to another sheet, copies the defined rows and inserts them into the last row of the current sheet.
My problem now is that the number that is being searched for in row K can also appear several times in the row, which means that the code now inserts the rows several times. I can't figure out how to rewrite it so that the counter stops as soon as it has found the number once in the row and thus the rows from the other sheet are only copied in once and not multiple times.
For the example in the code below. If "33527" is 5times in the row K than the rows 22:25 from the other sheet are copied and pasted 5 times into the current sheet but my goal is that it is only copied and pasted once regardless how many times the item is listed in row K.
I hope you get what I mean. Thank you for your help
My code below detects whether a certain number is in row K, if it finds it then it goes to another sheet, copies the defined rows and inserts them into the last row of the current sheet.
My problem now is that the number that is being searched for in row K can also appear several times in the row, which means that the code now inserts the rows several times. I can't figure out how to rewrite it so that the counter stops as soon as it has found the number once in the row and thus the rows from the other sheet are only copied in once and not multiple times.
For the example in the code below. If "33527" is 5times in the row K than the rows 22:25 from the other sheet are copied and pasted 5 times into the current sheet but my goal is that it is only copied and pasted once regardless how many times the item is listed in row K.
I hope you get what I mean. Thank you for your help
VBA Code:
Private Sub Wall()
Dim i As Long, lastrow1 As Long
Dim myname As String
lastrow1 = Sheets("Material").Range("K" & Rows.Count).End(xlUp).Row
For i = 2 To lastrow1
myname = "33527"
Application.ScreenUpdating = False
If Worksheets("Material").Cells(i, "K").Value = myname Then
Worksheets("stock").Activate
Worksheets("stock").Rows("22:25").Copy
Worksheets("Material").Activate
Sheets("Material").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End If
Application.CutCopyMode = False
Next i
Application.ScreenUpdating = True
End Sub