How to use macro to create a name

rickyckc

Active Member
Joined
Apr 1, 2004
Messages
327
Hi all,

I have a workbook call Sales.xls

In this workbook, there is a sheet call Sales Staff. In column C, there is a sales person name list. In C2, I want it to be blank. From C3 down-wards, are names of sales person. This name list are varied all the time, i.e. could be 10 people (C2:C12), could be 23 people (C2:25), etc. How to use macro to create a name for this (un-fixed) list ?

Currently, what I have is this, which is done by manually selection...

ActiveWorkbook.Names.Add Name:="SalesStaff", RefersToR1C1:= "='Sales Staff'!R2C3:R7C3"

Please help. Thanks.

Best Regards,
Ricky
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

roentgen

Board Regular
Joined
Apr 13, 2007
Messages
100
For a simple solution try:

Range("c3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.name = "SalesStaff"

This ignores c2 which you want blank and just names the list of staff names.
Each time this code runs, the full list is named irrespective of the number of entries.
Note that the list works down to the first blank, so there cannot be gaps in the list.
 
Upvote 0

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,961
Office Version
  1. 365
Platform
  1. Windows
No need to Select:

Code:
Sub test()
Range("C3:C" & Range("C3").End(xlDown).Row).Name = "SalesStaff"
End Sub
 
Upvote 0

Forum statistics

Threads
1,190,870
Messages
5,983,321
Members
439,839
Latest member
iblackie

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