Lastrow issue

erutherford

Active Member
Joined
Dec 19, 2016
Messages
449
Just when I thought I could write a little code! Why does the following code paste to row 268 instead of the last row?

VBA Code:
Private Sub CommandButton1_Click()
  ' Get the worksheets
    Dim shRead1 As Worksheet
    Dim shRead2 As Worksheet
    Dim shWrite As Worksheet
    Dim Lastrow As Long
    
    
   
    
    Lastrow = Range("A" & Rows.Count).End(xlUp).Row
        
    
    Set shRead1 = ThisWorkbook.Worksheets("Personnel")
    Set shRead2 = ThisWorkbook.Worksheets("This_year")
    Set shWrite = ThisWorkbook.Worksheets("Hstry")
    
       
    ' This will copy the values only
    
    shRead1.Range("P3:P66").Copy
    shWrite.Range("A2" & Lastrow).PasteSpecial xlPasteValues
    shRead2.Range("A2:G65").Copy
    shWrite.Range("B2" & Lastrow).PasteSpecial xlPasteValues
    
MsgBox "done"

End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
You need to remove the old row number (2) from your range references!

These references:
Range("A2" & Lastrow)
should look like this:
Range("A" & Lastrow)

Otherwise, you are tacking on 2 to the beginning of the row number (i.e. "68" becomes "268").
 
Upvote 0
You need to remove the old row number (2) from your range references!

These references:
Range("A2" & Lastrow)
should look like this:
Range("A" & Lastrow)

Otherwise, you are tacking on 2 to the beginning of the row number (i.e. "68" becomes "268").
Still starting at line 64?
VBA Code:
Private Sub CommandButton1_Click()
  ' Get the worksheets
    Dim shRead1 As Worksheet
    Dim shRead2 As Worksheet
    Dim shWrite As Worksheet
    Dim Lastrow As Long
    
    
   
    
    Lastrow = Range("A" & Rows.Count).End(xlUp).Row
        
    
    Set shRead1 = ThisWorkbook.Worksheets("Personnel")
    Set shRead2 = ThisWorkbook.Worksheets("This_year")
    Set shWrite = ThisWorkbook.Worksheets("Hstry")
    
       
    ' This will copy the values only
    
    shRead1.Range("P3:P66").Copy
    shWrite.Range("A" & Lastrow).PasteSpecial xlPasteValues
    shRead2.Range("A2:G65").Copy
    shWrite.Range("B" & Lastrow).PasteSpecial xlPasteValues
    
MsgBox "done"

End Sub
 
Upvote 0
What line should it be starting at?
Note that with your LastRow calculation, it is finding the last row in column A with data.
So if it is pasting to row 64, it means that there is something in cell A64.

Perhaps it would be better if you describe your data structure (or better yet, post a sample), and tell us exactly what you want to happen (instead of us guessing).
 
Upvote 0
What line should it be starting at?
Note that with your LastRow calculation, it is finding the last row in column A with data.
So if it is pasting to row 64, it means that there is something in cell A64.

Perhaps it would be better if you describe your data structure (or better yet, post a sample), and tell us exactly what you want to happen (instead of us guessing).
There is a header at A1. So the last row would be A2. I deleted Col A then added back, suspecting that there might be something that I couldn't see. The data structure is very simple as the code shows. Copy the data the range on shRead1 and paste it in the next row available on shWrite. I also deleted the "Hstry" sheet and redid it, same results.
 
Upvote 0
What sheet is the command button on?


What sheet are you referring to here?
The command button WAS on shRead2. I move it to wrksheet where the data is pasted (shWrite) and that seem to fix the "Line 64" paste. With the command button on the sheet where the data is pasted it is working fine. Why would the command btn location matter as long as things are pointed in the right direction?
 
Upvote 0
This is the code for the command button on the sheet where the data is pasted (shWrite)

VBA Code:
Private Sub CommandButton1_Click()
 'Get the worksheets
    Dim shRead1 As Worksheet
    Dim shRead2 As Worksheet
    Dim shWrite As Worksheet
    Dim Lastrow As Long
    
    Lastrow = Cells(Rows.Count, 1).End(xlUp).Row
     
    Set shRead1 = ThisWorkbook.Worksheets("Personnel")
    Set shRead2 = ThisWorkbook.Worksheets("This_year")
    Set shWrite = ThisWorkbook.Worksheets("Hstry")
      
    ' This will copy the values only
    
    shRead1.Range("P3:P66").Copy 'Reads current year
    shWrite.Range("A" & Lastrow + 1).PasteSpecial xlPasteValues
    shRead2.Range("A2:G65").Copy
    shWrite.Range("B" & Lastrow + 1).PasteSpecial xlPasteValues
    
MsgBox "done"

End Sub
 
Upvote 0
.. as long as things are pointed in the right direction?
The problem was that things were not pointed in the right direction
VBA Code:
 Lastrow = Range("A" & Rows.Count).End(xlUp).Row
This line of code does not specify which worksheet to use to find the last row. The default is that it will use the active sheet. That is the one that the button is (or was) on.

I think that if you put the button back on the original sheet, but move and change the Lastrow line like this it should work.

Rich (BB code):
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
 
Set shRead1 = ThisWorkbook.Worksheets("Personnel")
Set shRead2 = ThisWorkbook.Worksheets("This_year")
Set shWrite = ThisWorkbook.Worksheets("Hstry")

Lastrow = shWrite.Range("A" & Rows.Count).End(xlUp).Row
 
Upvote 0
Solution

Forum statistics

Threads
1,216,100
Messages
6,128,824
Members
449,470
Latest member
Subhash Chand

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