Using the range object (Error 424)

RobH1979

New Member
Joined
Mar 9, 2009
Messages
9
I have a combo box whose source is a named range ("variablelist"). However I need to update the named range every time new data is input into the spreadsheet.

I have therefore written the following code to update the named range. However when I run it I get 'ERROR 424 - Object Required 'on the SET statement. I have MS DAO 3.6 Object Library referenced. Does anyone know what is causing this error?

Dim rngVariable As Range
Dim strSet as String

strset = "Settings"

Sheets(strSet).Select
Range("A2").Select

Set rngVariable = Range(Selection, Selection.End(xlDown)).Select

ActiveWorkbook.Names.Add Name:="VariableList", RefersTo:=rngVariable
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,427
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Remove the .Select from the end of that line.
 
Upvote 0

Colin Legg

MrExcel MVP
Joined
Feb 28, 2008
Messages
3,497
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi,

Remove the select method from this line:
Rich (BB code):
Set rngVariable = Range(Selection, Selection.End(xlDown)) .Select

When we are working with ranges from VBA it is very rare that we actually need to select them. In fact, selecting ranges can cause problems and slow the code down, so we try to avoid selecting when we can! This code should produce the same result without selecting:

Rich (BB code):
Sub test()
    Dim rngVariable As Range
 
    With Sheets("Settings")
        Set rngVariable = .Range("A2", .Range("A2").End(xlDown))
    End With
 
    ThisWorkbook.Names.Add Name:="VariableList", RefersTo:=rngVariable
End Sub

Hope that helps
 
Upvote 0

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,427
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Or even:
Code:
Sub test()
    With Sheets("Settings")
        .Range("A2", .Range("A2").End(xlDown)).Name = "VariableList"
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,191,584
Messages
5,987,479
Members
440,097
Latest member
Wint

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
Top