run-time error 9

David Cates

New Member
Joined
Mar 10, 2009
Messages
9
the following procedure produces a run-time error 9: subscript out of range
on the line r1 = Worksheets("Addresses").Range("A2").CurrentRegion.Select

What is wrong? Using Excel 2007


Public Sub EnterDataInWorksheet()
' Copies data from the user form
' to the next blank row in the worksheet.
Dim Addresses As Worksheet
Dim r As Range, r1 As Range


r1 = Worksheets("Addresses").Range("A2").CurrentRegion
Set r1 = r.Offset(r.Rows.Count, 0)
r1.Cells(1).Value = txtFirstName.Value
r1.Cells(2).Value = txtLastName.Value
r1.Cells(3).Value = txtAddress.Value
r1.Cells(4).Value = txtCity.Value
r1.Cells(5).Value = cmbStates.Value
r1.Cells(6).Value = txtZip.Value
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try

Rich (BB code):
Set r = Worksheets("Addresses").Range("A2").CurrentRegion
 
Upvote 0
As Peter indicated, when assigning a reference to an object variable (such as a range, a worksheet, a workbook, or a shape) you need to use the Set keyword. When assigning a simple scalar value to a variable (such as a string or an integer) you usse the Let keyword - but because this is the default when assigning values, you can actually omit it, which is why you rarely ever see anyone use it in normal module code.
 
Upvote 0
Do you have a sheet named exactly Addresses without leading or trailing spaces?
 
Upvote 0
error 9: subscript out of range
on the line r1 = Worksheets("Addresses").Range("A2").CurrentRegion.Select

What is wrong? Using Excel 2007
even with set r = ....., a runtime error is still produced!
1. Make sure you also removed the Select method from the end of the line as VoG indicated in post #2.
2. Make sure that "Addresses" is a valid worksheet name within the same workbook where the code resides. Especially check for any spaces before or after the name.

Hope that helps...
 
Upvote 0

Forum statistics

Threads
1,203,472
Messages
6,055,612
Members
444,803
Latest member
retrorocket129

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