VBA code to select a range using a mouse by hilighting range

banjoe

Board Regular
Joined
Oct 30, 2002
Messages
112
I use the following snippet of code to prompt the user to type in a range from a message box which then becomes a named range in my spreadsheet. How can I alter the code to have the user select the range with his mouse instead of having to type it in the message box?

Code:
SpecifiedRange = InputBox("Specifiy the range to be named: 'Table'")
Range(SpecifiedRange).Select
MsgBox "This is the range you selected"
SpecifiedRange = Selection.Address
ActiveWorkbook.Names.Add Name:="Table", _
RefersTo:="=" & SpecifiedRange

Any help would be much appreciated. Banjoe.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Jindon:
Please bear with me a little - I'm still very new to VBA. I altered my original code (see below), and although the new code now allows me to select a range, that range does not become a named range as I intended. Whatever the active cell happens to be is what becomes named.

Code:
Specifiedrange = Application.InputBox("Specifiy the range to be named: 'Table'", Type:=8)
Range(Specifiedrange).Select
MsgBox "This is the range you selected"
Specifiedrange = Selection.Address
ActiveWorkbook.Names.Add Name:="Table", _
RefersTo:="=" & Specifiedrange

I need some more help to execute your suggestion. Also, I need some help to get the following lines of my code to work:
Code:
Range(Specifiedrange).Select
MsgBox "This is the range you selected"
The 2 above lines basically are supposed to confirm the range selected by the user.

Thanks for your help so far, I appreciate your patience with me. Banjoe.
 
Upvote 0
try this way

Code:
Dim Specifiedrange As Range
Set Specifiedrange = Application.InputBox("Specifiy the range to be named: 'Table'", Type:=8)
MsgBox "This is the range you selected " & Specifiedrange.Address
Sepcifiedrange.Name = "Table"
 
Upvote 0
Jindon:
I used the code you posted but I am appearently doing something wrong because I get the following error:

Run-time error '424'
Object required

It "errors out" at the follow line:

Code:
Sepcifiedrange.Name = "Table"

What am I doing wrong?
Banjoe.
 
Upvote 0
Jindon:
Success! It worked great. I should have caught that typo myself. I really, really appreciate your help with my question. Thanks soooo much. Banjoe.
 
Upvote 0

Forum statistics

Threads
1,214,867
Messages
6,122,002
Members
449,059
Latest member
mtsheetz

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