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 fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
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,223,244
Messages
6,170,976
Members
452,372
Latest member
Natalie18

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