Set command error...

dplum

Board Regular
Joined
Nov 1, 2006
Messages
217
I'm getting an error on the statement "Set vRefersto_Cell_Range = vSheet_Range".

I believe that "vRefersto_Cell_Range" is needed for the "Names.Add" statement

Any help is much appreciated. BTW, I realize that a Name can be easily assigned using the Name box, but I've created this macro to simply test the use of Names.Add statement.

Code:
Sub NameSelectedCells()
    Dim vInputCells, vRefersto_Cell_Range As Range
    Set vInputCells = Application.InputBox(prompt:="Select cells to be named...", Left:=1, Top:=1, Type:=8)
    'type:=8 for cell references or ranges; change the Left and Top arguments to position the InputBox
    vSheet_Range = "'" & ActiveSheet.Name & "'!" & vInputCells.Address(ReferenceStyle:=xlA1)
    Set vRefersto_Cell_Range = vSheet_Range
    Do
        vRangeName = InputBox("Assign the Range[" & vSheet_Range & "]" & vbCr & _
                              "to the following Name...")
        If vRangeName = "" Then Exit Sub
        On Error Resume Next
        Names.Add Name:=vRangeName, RefersTo:=vRefersto_Cell_Range, Visible:=True
        If Err.Number = 0 Then Exit Sub
        MsgBox "RangeName... [" & vRangeName & "] is Invalid!" & vbCr & _
            "Must Not start with a Number, Must Not contain a Space, Only underscore (_) special character is allowed"
    Loop
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I'm getting an error on the statement "Set vRefersto_Cell_Range = vSheet_Range".

I believe that "vRefersto_Cell_Range" is needed for the "Names.Add" statement

Any help is much appreciated. BTW, I realize that a Name can be easily assigned using the Name box, but I've created this macro to simply test the use of Names.Add statement.

Code:
Sub NameSelectedCells()
    Dim vInputCells, vRefersto_Cell_Range As Range
    Set vInputCells = Application.InputBox(prompt:="Select cells to be named...", Left:=1, Top:=1, Type:=8)
    'type:=8 for cell references or ranges; change the Left and Top arguments to position the InputBox
    vSheet_Range = "'" & ActiveSheet.Name & "'!" & vInputCells.Address(ReferenceStyle:=xlA1)
    Set vRefersto_Cell_Range = vSheet_Range
    Do
        vRangeName = InputBox("Assign the Range[" & vSheet_Range & "]" & vbCr & _
                              "to the following Name...")
        If vRangeName = "" Then Exit Sub
        On Error Resume Next
        Names.Add Name:=vRangeName, RefersTo:=vRefersto_Cell_Range, Visible:=True
        If Err.Number = 0 Then Exit Sub
        MsgBox "RangeName... [" & vRangeName & "] is Invalid!" & vbCr & _
            "Must Not start with a Number, Must Not contain a Space, Only underscore (_) special character is allowed"
    Loop
End Sub

Try :

Code:
 Set vRefersto_Cell_Range = Range(vSheet_Range)
 
Upvote 0
You've Dim'd vRefersto_Cell_Range as a Range, but you're trying to assign a string to it.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,286
Members
452,902
Latest member
Knuddeluff

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