PC Review


Reply
Thread Tools Rate Thread

Creating a named range in a workbook using VBA

 
 
=?Utf-8?B?R2VyYWxkTQ==?=
Guest
Posts: n/a
 
      27th Feb 2007
I have added a combo-box control to a worksheet.
The ListFillRange for the combo-box is a named range
(DespList1!AllDepartments)
This works OK.

The problem I have is the size of the list will vary over time (get bigger).
Currently I have defined the named range to include a larger range of cells
than are actually used to accomodate growth. This means the named range
includes blank entries.

I would prefer not to have blank entries in the range because the combo-box
then shows the blank rows at the bottom of the list (and looks
unprofessional).

To avoid this i tried writing a macro to re-define the named range to the
actual list size. See code below. The macro executes without error but the
named range is not set in the workbook.

Q1 Am I trying to do something that cannot be done?
Q2 If so does anyone know of an alternative?

Macro code:

Sub SetDepartmentList()

Dim varLastRec as Variant

Sheets("Lookup").Select
Application.Goto Reference:="R65000C2"
Selection.End(xlUp).Select
varLastRec = Selection.Address
Range("A2").Select

ActiveWorkbook.Names.Add Name:="AllDepartments", _
RefersTo:="=Lookup!$A$1:varLastRec"

End Sub

--
-- Thanks in advance
 
Reply With Quote
 
 
 
 
=?Utf-8?B?VmVyZ2VsIEFkcmlhbm8=?=
Guest
Posts: n/a
 
      27th Feb 2007
Gerald,

Assuming your list is in Sheet1, starting from cell A1 going down, then you
can use this to define your named range:

=OFFSET(Sheet1!$A$1,0,0,COUNTA(Sheet1!$A:$A), 1)



"GeraldM" wrote:

> I have added a combo-box control to a worksheet.
> The ListFillRange for the combo-box is a named range
> (DespList1!AllDepartments)
> This works OK.
>
> The problem I have is the size of the list will vary over time (get bigger).
> Currently I have defined the named range to include a larger range of cells
> than are actually used to accomodate growth. This means the named range
> includes blank entries.
>
> I would prefer not to have blank entries in the range because the combo-box
> then shows the blank rows at the bottom of the list (and looks
> unprofessional).
>
> To avoid this i tried writing a macro to re-define the named range to the
> actual list size. See code below. The macro executes without error but the
> named range is not set in the workbook.
>
> Q1 Am I trying to do something that cannot be done?
> Q2 If so does anyone know of an alternative?
>
> Macro code:
>
> Sub SetDepartmentList()
>
> Dim varLastRec as Variant
>
> Sheets("Lookup").Select
> Application.Goto Reference:="R65000C2"
> Selection.End(xlUp).Select
> varLastRec = Selection.Address
> Range("A2").Select
>
> ActiveWorkbook.Names.Add Name:="AllDepartments", _
> RefersTo:="=Lookup!$A$1:varLastRec"
>
> End Sub
>
> --
> -- Thanks in advance

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Named Range From One Workbook Used in Validation Drop Down in 2nd Workbook Minitman Microsoft Excel Misc 1 19th Aug 2008 06:49 AM
Named range by VBA to another workbook Madiya Microsoft Excel Programming 5 1st Aug 2007 02:22 AM
Workbook named range not seen by ADO when workbook closed =?Utf-8?B?VGltIEx1bmQ=?= Microsoft Excel Programming 2 16th Mar 2007 08:59 AM
creating a filtered range/named range mark kubicki Microsoft Excel Programming 1 4th Nov 2006 03:14 PM
Can I use named range in data range box when creating pie chart? =?Utf-8?B?QkphY2tzb24=?= Microsoft Excel Charting 2 17th Aug 2005 05:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:22 AM.