VBA Code to copy rows from one sheet to another dynamically

Veni11

New Member
Joined
Oct 20, 2023
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Hello everyone

I'm trying to copy and paste rows from one sheet to another when a specific item is mentioned in the sheet. The following code is used multiple times for different items. I tried it with offset because i couldn't figure it out how to write the code so that the copied rows are always pasted under the last row without a gap. the sheet "Material" in which the rows are inserted is dynamic. Without the offset i had difficulties because it overwrote the newly inserted rows and didn't recognize the new last row.

I hope someone has some tips. Thank you.

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(27, 0).PasteSpecial xlPasteValues
    End If

Application.CutCopyMode = False
Next i

Application.ScreenUpdating = True


End Sub
 
You are welcome!
May I ask you one last question to this topic. When my main sheet contains the Item number 10 times than code copies the row 10 times in, is there a easy way to change that it doesnt matter how many times in Row K the item is found it should only copy and paste it one time to my sheet..? Sorry and thank you for your feedback
 
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
May I ask you one last question to this topic. When my main sheet contains the Item number 10 times than code copies the row 10 times in, is there a easy way to change that it doesnt matter how many times in Row K the item is found it should only copy and paste it one time to my sheet..? Sorry and thank you for your feedback
That is a much bigger more complex issue that would require its own thread.
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,987
Members
449,093
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top