Copy data from row 2 up to last used row and save to last available row in another sheet

HR Jonas

New Member
Joined
Nov 17, 2021
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Hi. I need some help with my macro.

I have 2 sheets. Sheet 1 is my working paper with more than 20 columns and Sheet 2 is my database.

Whenever I enter some data in my sheet 1 to do some calculations, I want to save the values to my database (Sheet 2) to its specified columns. The problem is the number of rows in my sheet 1 always vary. How can I copy my data from row 2 up to last used row and save to it to the last available row in my database.

Below is the code I am using:

Sub SavePayroll

Dim lastrow as long
Dim erow as long

lastrow = sheet1.cells(rows.count,5).end(xlup).row 'I used column 5 or E to be the basis of finding the last used row

erow = sheet2.cells(rows.count,1).end(xlup).row + 1

For i = 2 to lastrow

Sheet2.cells(erw, 1)= range("B" & i)
Sheet2.cells(erw, 2)= range("C" & i)

Next
End Sub

Using the above code, only the value of the last row was saved to my database. Thank you so much for the help.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try
VBA Code:
Sheet2.cells(erw, 1).Resize(lastrow-1).value= range("B2:B" & lastrow).Value
 
Upvote 0
Try
VBA Code:
Sheet2.cells(erw, 1).Resize(lastrow-1).value= range("B2:B" & lastrow).Value
Hi. I tried the code but it's not working. It only copy and transfer the value of the last row.

Can you show me how to incorporate this code to my current code?
 
Upvote 0
How about
VBA Code:
Sub SavePayroll()

Dim lastrow As Long
Dim erow As Long

lastrow = Sheet1.Cells(Rows.Count, 5).End(xlUp).Row 'I used column 5 or E to be the basis of finding the last used row

erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Row + 1

Sheet2.Cells(erow, 1).Resize(lastrow - 1, 2).Value = Range("B2:C" & lastrow).Value
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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