Run-time error '9': Subscript out of range

Hazelwood

New Member
Joined
Feb 25, 2018
Messages
13
I get the "Run-time error '9': Subscript out of range" error when running my macro from a Button assigned to my macro, but if I run my macro from the ribbon it works fine. (When I debug, it says the problem is with the "LastRow =" line. Don't understand why.

Table256 is the only table and only data on the Members tab - my row is successfully added to my table

I'm running this on a mac ... Looking for some help to understand problem

<style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 ; background-color: #ffffff }p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008f00 ; background-color: #ffffff }p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; background-color: #ffffff ; min-height: 13.0px}span.s1 {color: #011993}</style>Sub GetLastRow()


Dim sht As Worksheet
Dim LastRow As Long
Set sht = ActiveSheet

Sheets("Members").Select

ActiveSheet.ListObjects("Table256").ListRows.Add AlwaysInsert:=True

LastRow = sht.ListObjects("Table256").Range.Rows.Count

End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi & welcome to the board
Try
Code:
Sub GetLastRow()


Dim sht As Worksheet
Dim LastRow As Long
Set sht = Sheets("Members")


sht.ListObjects("Table256").ListRows.Add AlwaysInsert:=True

LastRow = sht.ListObjects("Table256").Range.Rows.Count

End Sub
Your problem was probably due the the Members sheet not being the active sheet when you ran the macro
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,106
Messages
6,123,124
Members
449,097
Latest member
mlckr

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