Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Ant Ant is offline
external usenet poster
 
Posts: 53
Default Select range then name it

Hi,

Looking for code that will select/highlight a range and then name it. The
range is an Excel download that varies in size. A user can run several
downloads per day hence the need to automate the process. It would be cool to
have some kind of pop up box that allows the user to type in a name each time.

Thanks.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default Select range then name it

Try:
sub definename()
activecell.currentregion.select
Application.Dialogs(xlDialogDefineName).Show
end sub
This will select the current region of the active cell and open the
define names dialog; the user can just type the name in and hit enter
to name the range.

Caveats:
1. This aassumes that the active cell is in the downloaded range.
2. You'll have to develop this further if you have multiple downloaded
ranges in contiguous ranges on the same sheet. This will work as long
as each download is separated from others.

Note: Depending on how the info is downloaded, the range may already
be named. (i.e. MS Query) Check your name drop-down. This would make
it a lot easier to work with multiple contiguous downloaded ranges.

Cliff Edwards

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default Select range then name it

Or if you can get the user to select the downloaded range you can use
something like this:

Dim sRange As Range
Dim newName As String

Sub defineRange()
Set sRange = Application.InputBox("Select the range.", "Name
Downloaded Range.", Selection.Address, , , , , 8)
newName = Application.InputBox("Enter the new name.")
ActiveWorkbook.Names.Add name:=newName, RefersToR1C1:=sRange
End Sub

Cliff Edwards

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default Select range then name it

code didn't wrap right -

Set sRange = Application.InputBox("Select the range.", "Name
Downloaded Range.", Selection.Address, , , , , 8)

is one line


  #5   Report Post  
Posted to microsoft.public.excel.programming
Ant Ant is offline
external usenet poster
 
Posts: 53
Default Select range then name it

Hi Cliff.

Thanks for your reply(s). I think I am almost there. I prefer
ActiveCell.CurrentRegion.Select as it sets the range automatically. However I
must have something missing as it does everything but name the range. Can you
see what I am missing?

Dim newName As String

Sub defineRange()
ActiveCell.CurrentRegion.Select
newName = Application.InputBox("Enter the new name.")
End Sub

"ward376" wrote:

Or if you can get the user to select the downloaded range you can use
something like this:

Dim sRange As Range
Dim newName As String

Sub defineRange()
Set sRange = Application.InputBox("Select the range.", "Name
Downloaded Range.", Selection.Address, , , , , 8)
newName = Application.InputBox("Enter the new name.")
ActiveWorkbook.Names.Add name:=newName, RefersToR1C1:=sRange
End Sub

Cliff Edwards




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Select range then name it

Sub defineRange()
Dim newName As String
ActiveCell.CurrentRegion.Select
newName = Application.InputBox("Enter the new name.")
if newname = "" then
exit sub
end if
activecell.currentregion.name = newname

End Sub

Ant wrote:

Hi Cliff.

Thanks for your reply(s). I think I am almost there. I prefer
ActiveCell.CurrentRegion.Select as it sets the range automatically. However I
must have something missing as it does everything but name the range. Can you
see what I am missing?



Sub defineRange()
ActiveCell.CurrentRegion.Select
newName = Application.InputBox("Enter the new name.")
End Sub

"ward376" wrote:

Or if you can get the user to select the downloaded range you can use
something like this:

Dim sRange As Range
Dim newName As String

Sub defineRange()
Set sRange = Application.InputBox("Select the range.", "Name
Downloaded Range.", Selection.Address, , , , , 8)
newName = Application.InputBox("Enter the new name.")
ActiveWorkbook.Names.Add name:=newName, RefersToR1C1:=sRange
End Sub

Cliff Edwards



--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default Select range then name it

Option Explicit
Dim sRange As String
Dim newName As Variant

Sub defineRange()
sRange = ActiveCell.CurrentRegion.Address
newName = Application.InputBox("Enter the new name.")
If newName = "" Then
Exit Sub
ElseIf newName = False Then
Exit Sub
ElseIf IsNumeric(newName) Then
Exit Sub
End If
ActiveWorkbook.Names.Add Name:=newName, RefersToR1C1:=sRange
End Sub

Notes: That was a bad example, me declaring an inputbox as a string;
it should always be a variant. It can be a string, boolean or numeric.
I may still be overlookinga data type there.

Cliff Edwards


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can change range to select active rows instead of :=Range("S10 ldiaz Excel Discussion (Misc queries) 7 August 29th 08 03:52 PM
When entering data into a range of cells, select the entire range. Q Excel Discussion (Misc queries) 0 September 26th 07 04:36 AM
select range and put range address in variable [email protected] Excel Programming 2 January 25th 06 01:28 AM
Compare a selected Range with a Named range and select cells that do not exist PCLIVE Excel Programming 1 October 18th 05 07:09 PM
Select Sheet then Select Range Gee[_2_] Excel Programming 3 May 27th 04 10:10 PM


All times are GMT +1. The time now is 08:29 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"