Why is my variable losing its value?

gbell3587

Board Regular
Joined
Jan 30, 2011
Messages
117
Hi

Im constantly copying & pasting data from 1 sheet to another & then i need to find the last row & add 1 to it so when i copy/paste the next set of data, it starts from the next row however the variable lrow always returns 1, like it resets its value or never changes to the actual last row. Any ideas? Its driving me mad!
VBA Code:
sum example()
dim lrow as long
lrow = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = True
'
    Sheets("MTB").Select
    Range("I6:J212").Select
    Selection.Copy
    Sheets("Sheet2").Select
    Range("A1").Select
        MsgBox lrow 'at this point, lrow returns 1 (which is fine)
    Selection.PasteSpecial Paste:=xlPasteValues
 lrow = lrow + 1 'lrow now returns 2 (should be 213)
    MsgBox lrow
    Range("A" & lrow).Select
    MsgBox lrow
end sub
 
Last edited:

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Works for me. lrow did not report 1.
Hi

Im constantly copying & pasting data from 1 sheet to another & then i need to find the last row & add 1 to it so when i copy/paste the next set of data, it starts from the next row however the variable lrow always returns 1, like it resets its value or never changes to the actual last row. Any ideas? Its driving me mad!
VBA Code:
sum example()
dim lrow as long
lrow = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = True
'
    Sheets("MTB").Select
    Range("I6:J212").Select
    Selection.Copy
    Sheets("Sheet2").Select
    Range("A1").Select
        MsgBox lrow 'at this point, lrow returns 1 (which is fine)
    Selection.PasteSpecial Paste:=xlPasteValues
 lrow = lrow + 1 'lrow now returns 2 (should be 213)
    MsgBox lrow
    Range("A" & lrow).Select
    MsgBox lrow
end sub
I have gaps in column I & J, so there are a lot of blanks when it copies - do you think this is throwing something out?

Works for me. lrow did not report 1.
I have gaps in column I & J, do you think this is throwing something out? Ive tried this in a fresh sheet but i still cant get lrow above 2!

See image & new code

VBA Code:
Sub example()
Dim lrow As Long
lrow = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = True
'
    Sheets("MTB").Select
    Range("a6:b212").Select
    Selection.Copy
    Sheets("Sheet2").Select
    Range("A1").Select
        MsgBox lrow 'at this point, lrow returns 1 (which is fine)
    Selection.PasteSpecial Paste:=xlPasteValues
 lrow = lrow + 1 'lrow now returns 2 (should be 213)
    MsgBox lrow
    Range("A" & lrow).Select
    MsgBox lrow
End Sub
 

Attachments

  • Capture.JPG
    Capture.JPG
    41.1 KB · Views: 4
Upvote 0
Based on the comments in your code, it appears that you do not understand.
VBA Code:
        MsgBox lrow 'at this point, lrow returns 1 (which is fine)
    Selection.PasteSpecial Paste:=xlPasteValues
 lrow = lrow + 1 'lrow now returns 2 (should be 213)
    MsgBox lrow

The first msgbox, your comment says lrow = 1.
Two lines later you add 1 to lrow and expect it to = 213?

If you want to update lrow, you have to run the calculation again, lrow = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row for example.
 
Upvote 0
Try doing it like this:
VBA Code:
Sub My_Script()
'Modified  7/1/2022  12:56:53 PM  EDT
Application.ScreenUpdating = False
Dim lrow As Long
lrow = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row + 1
Sheets("MTB").Range("I6:J212").Copy
Sheets("Sheet2").Range("A" & lrow).PasteSpecial Paste:=xlPasteValues
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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