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. In this workbook, I have a sheet call Sales Staff. In this sheet, I have a column of sales person's names. This list of sales names are grab from a database and is always not the same amount of people. I need a macro to do variable selection of the sales staff names, in column C, to help create a name call SalesStaff.

Currently what I have is manual selection...

[/code]
ActiveWorkbook.Names.Add Name:="SalesStaff", RefersToR1C1:= _
"='Sales Staff'!R2C3:R7C3"
Code:
Thanks.

Best Regards,
Ricky
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
Hi Ricky

I assume you want the name to expand to the size of the data? Perhaps then:

Code:
ActiveWorkbook.Names.Add Name:="SalesStaff", RefersToR1C1:= _ 
"='Sales Staff'!R2C3:R" & Sheets("Sales Staff").Cells(Rows.Count,"C").End(xlUp).Row & "C3"
 
Upvote 0

rickyckc

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

Thanks. I got it figured myself and got it working. What I need now is a line to check whether a name called 'SalesStaff' exist. If exist, delete it. If does not exist, do nothing and carry on with next macro line.

Likewise a line to check whether a sheet call Sales Order exist. If exist, do nothing. If does not exist, create it.

Thanks.

Best Regards,
Ricky
 
Upvote 0

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
Maybe:

Code:
On Error Resume Next
Names("MyName").Delete
On Error Goto 0

'worksheet stuff

On Error Resume Next
Err.Clear
Set ws = Worksheets("Sales Order")
If Err <> 0 Then
   Set ws = Worksheets.Add
   ws.Name = "Sales Order"
End If
On Error GoTo 0
 
Upvote 0

Forum statistics

Threads
1,190,809
Messages
5,983,040
Members
439,815
Latest member
yoswosz

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