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
 
It goes below where you set shWrite because shWrite is now used in the Lastrow line.

It does not have to go below. If you really wanted Lastrow to go above where the shWrite worksheet is set, then you would need to specify the worksheet like this

Rich (BB code):
Lastrow = ThisWorkbook.Worksheets("Hstry").Range("A" & Rows.Count).End(xlUp).Row
 
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,216,099
Messages
6,128,816
Members
449,469
Latest member
Kingwi11y

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