VBA help with copy pasting rows

vbahelpneeded15

New Member
Joined
Jun 28, 2017
Messages
12
Hi,
I have been trying to set up a VBA code that helps me move rows with the word Sam in column A to the bottom of the sheet (the number of rows with this word changes everyday). I have managed to set up a code that copies the data but it always pastes it in the same row (i.e. row 540) even if it is not the next empty row. I was wondering if I could get some help with it so it pastes in the next empty row instead.
Here's my code:

Sub Step3 ()
Dim r As Range
Dim s As String
Dim firstRowWithS As Long, lastRowWithS As Long, destinationRow As Long

s = "Sam"
destinationRow = 540

For Each r In Sheets("sheetname").Range("A373:A398")
'find first row where we see string s
If r.value = s And r.Offset(-1, 0).value <> s Then
firstRowWithS = r.Row
End If

'find last row where we see string s
If r.value <> s And r.Offset(-1, 0).value = s Then
lastRowWithS = r.Offset(-1, 0).Row
End If
Next

'copy and paste rows to destination
Sheets("sheetname").Range(373 & ":" & 398).Copy Destination:=Sheets("sheetname").Range("A" & destinationRow)
End Sub

Thank you for the help!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hia
Try changing this
Code:
destinationRow = 540
to
Code:
Sheets("sheetname").Range(373 & ":" & 398).Copy Destination:=Sheets("sheetname").Range("A" &  Rows.Count).End(xlUp).Row
 
Last edited:
Upvote 0
In pasting, you typically only need to designate the first cell of the paste range.
Do you have any merged cells in either range (from/to)?

Also, I am not sure why you are using this:
Code:
Range(373 & ":" & 398)
instead of just using this:
Code:
Range("373:398")

Seems a bit unnecessary if that is a hard-coded range.
 
Upvote 0
Hi! I don't have any merged cells. That's what I did earlier to paste, but the issue is that the cell I want to paste in changes everyday (I want it to paste it in the first blank row that day). I'm not entirely sure how to write a code that does that.
 
Upvote 0
Apologies it should read
Code:
Sheets("sheetname").Range(373 & ":" & 398).Copy Destination:=Sheets("sheetname").Range("A" &  Rows.Count).End(xlUp).Offset(1)
This will however paste all rows 373 to 398 regardless.
should this line be
Code:
Sheets("sheetname").Range(firstRowWithS & ":" & lastRowWithS).Copy  Destination:=Sheets("sheetname").Range("A" &   Rows.Count).End(xlUp).Offset(1)
 
Last edited:
Upvote 0
So, you want to determine the destination row dynamically, by selecting the first blank cell in column A after the last row of data?
You can do that like this:
Code:
[COLOR=#333333]destinationRow = [/COLOR][COLOR=#333333]Sheets("sheetname").Cells(Rows.Count,"A").End(xlUp).Row+1[/COLOR]
 
Upvote 0
Just change the .Copy in your VBA code to .Cut
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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