VBA entry in last row

Mohsin110

New Member
Joined
Aug 26, 2023
Messages
23
Office Version
  1. 2021
Platform
  1. Windows
Hi, Please review my small code, is there any problem, i just want to enter two values to second sheet from 1st sheet. but it's not working with following code. sharing just part of code.

VBA Code:
Sub SaveButton()
    Dim sh, sh2, sh3 As Worksheet
    Dim LR, LRB, ID, l, i As Long

    Set sh = ThisWorkbook.Sheets("MRN")              'Sheet 1
    Set sh2 = ThisWorkbook.Sheets("mrnData")       'Sheet 2
    Set sh3 = ThisWorkbook.Sheets("SubmitCS")     'Sheet 3

    LR = sh2.Cells(Rows.Count, 1).End(xlUp).Row + 1    'Last row of sheet 2
    LRB = sh3.Cells(Rows.Count, 1).End(xlUp).Row + 1  'Last row of sheet 3

    For i = 8 To LRB    'entering to last empty row starting from 8th row
        With sh3
           .Cells(LRB, 1) = Range("Date").Value
           .Cells(LRB, 2) = Range("ID").Value
        End With
     Next i

 End Sub
 

Attachments

  • img2.png
    img2.png
    10.1 KB · Views: 8

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I think in the bit below, you could do with swapping LRB for i:
VBA Code:
        With sh3
           .Cells(LRB, 1) = Range("Date").Value
           .Cells(LRB, 2) = Range("ID").Value
        End With

You could also do it without a loop:
VBA Code:
    With sh3
       .Range(.Cells(8, 1), .Cells(LRB, 1)) = Range("Date").Value
       .Range(.Cells(8, 2), .Cells(LRB, 2)) = Range("ID").Value
    End With
 
Upvote 0
It won't cause an error (normally) but also your declarations are wrong for VBA if you want all of the lines below to be worksheets
VBA Code:
Dim sh, sh2, sh3 As Worksheet
and all the below to be Longs
VBA Code:
Dim LR, LRB, ID, l, i As Long
You need to explicitly declare each Variable in VBA or else they are Variants i.e. it should be
VBA Code:
    Dim sh As Worksheet, sh2 As Worksheet, sh3 As Worksheet
    Dim LR As Long, LRB As Long, ID As Long, l As Long, i As Long
 
Upvote 0
I think in the bit below, you could do with swapping LRB for i:
VBA Code:
        With sh3
           .Cells(LRB, 1) = Range("Date").Value
           .Cells(LRB, 2) = Range("ID").Value
        End With

You could also do it without a loop:
VBA Code:
    With sh3
       .Range(.Cells(8, 1), .Cells(LRB, 1)) = Range("Date").Value
       .Range(.Cells(8, 2), .Cells(LRB, 2)) = Range("ID").Value
    End With
Thanks for guiding me, i swapped LRB with i as you suggest, but it's entering data to all rows each time, not just in one last row. Please check.

VBA Code:
  For i = 8 To LRB
        With sh3
           .Cells(i, 1) = Range("Date").Value
           .Cells(i, 2) = Range("ID").Value + 1
        End With
  Next i
 

Attachments

  • img3.png
    img3.png
    14.4 KB · Views: 6
  • img4.png
    img4.png
    15.8 KB · Views: 5
Upvote 0
Yes sorry that is what i thought you wanted to do.

In the OP it states "two values to second sheet from 1st sheet" but your code seems to set up to move values to the third sheet, could that be the issue?

I just tested the below (with MARK858's changes) and it seems to function correctly:
VBA Code:
Sub SaveButton()
    Dim sh2 As Worksheet, sh3 As Worksheet
    Dim LRB As Long, ID As Long

    Set sh2 = ThisWorkbook.Sheets("mrnData")       'Sheet 2
    Set sh3 = ThisWorkbook.Sheets("SubmitCS")     'Sheet 3

    LRB = sh3.Cells(Rows.Count, 1).End(xlUp).Row + 1  'Last row of sheet 3

    With sh3
       .Cells(LRB, 1) = Range("Date").Value
       .Cells(LRB, 2) = Range("ID").Value + 1
    End With
 End Sub
 
Upvote 1
Solution
Yes sorry that is what i thought you wanted to do.

In the OP it states "two values to second sheet from 1st sheet" but your code seems to set up to move values to the third sheet, could that be the issue?

I just tested the below (with MARK858's changes) and it seems to function correctly:
VBA Code:
Sub SaveButton()
    Dim sh2 As Worksheet, sh3 As Worksheet
    Dim LRB As Long, ID As Long

    Set sh2 = ThisWorkbook.Sheets("mrnData")       'Sheet 2
    Set sh3 = ThisWorkbook.Sheets("SubmitCS")     'Sheet 3

    LRB = sh3.Cells(Rows.Count, 1).End(xlUp).Row + 1  'Last row of sheet 3

    With sh3
       .Cells(LRB, 1) = Range("Date").Value
       .Cells(LRB, 2) = Range("ID").Value + 1
    End With
 End Sub
It's working now, thanks for solving my problem.
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,143
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