VB Code not working right (Post values to first available row)

Raustin

Board Regular
Joined
Mar 21, 2011
Messages
50
Hello,
I have this code that is not working exactly right. I am attempting to get some values of one sheet, and post them to a table on a second sheet. On the first sheet they are copied from specific cells each time. They are being copied to a table on the 2nd sheet in which i need the values copied to the first available row. As of now they are being copied after the last line of values (even if there are rows in between which have no values). Basically. i have a last "Totals" row at the bottom of this table, and i must have the values posted to rows above this line, not below. Here is my current code:
Code:
Private Sub CommandButton1_Click()
Dim lst As Long
Dim CurrRw As Long
lst = Sheet5.Range("a" & Rows.Count).End(xlUp).Row + 1
If ActiveSheet.Name <> "Invoice" Then Exit Sub
Sheet5.Cells(lst, 1) = Range("g10")
Sheet5.Cells(lst, 2) = Range("j4")
Sheet5.Cells(lst, 3) = Range("j41")
Sheet5.Cells(lst, 4) = Range("n38")
Sheet5.Cells(lst, 5) = Range("n41")
Sheet5.Cells(lst, 6) = Range("j5")
If ActiveSheet.Name <> "Invoice" Then Exit Sub
If lst >= 20 Then 'alter to be the last row of table
    MsgBox "Table Limit Reached!!, data will not be copied"
    Exit Sub
End If
End Sub

Thank you for your help,
Randy
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Perhaps something like this:-
Code:
Dim lst As Long
lst = sheets("Sheet5").Range("A1").End(xlDown).row + 1
MsgBox lst
 
Upvote 0
Hi,
When i replaced the line, i got a 'Subscript out of range' error.
Here is the current code:
Code:
Private Sub CommandButton1_Click()
MsgBox "Invoice Posted"
Dim lst As Long
[COLOR="Red"]lst = Sheets("Sheet5").Range("A2").End(xlDown).Row + 1[/COLOR]
If ActiveSheet.Name <> "Invoice" Then Exit Sub
Sheet5.Cells(lst, 1) = Range("g10")
Sheet5.Cells(lst, 2) = Range("j4")
Sheet5.Cells(lst, 3) = Range("j41")
Sheet5.Cells(lst, 4) = Range("n38")
Sheet5.Cells(lst, 5) = Range("n41")
Sheet5.Cells(lst, 6) = Range("j5")
If ActiveSheet.Name <> "Invoice" Then Exit Sub
If lst >= 20 Then 'alter to be the last row of table
    MsgBox "Table Limit Reached!!, data will not be copied"
    Exit Sub
End If
End Sub
 
Upvote 0
So, you need to insert a line before doing your copy.

Something like:
Code:
Range("A1").Select
Selection.End(xldown).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

This should take you to the total line, and insert a blank line.
 
Upvote 0

Forum statistics

Threads
1,214,972
Messages
6,122,530
Members
449,088
Latest member
RandomExceller01

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