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

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
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
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
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,214,787
Messages
6,121,569
Members
449,038
Latest member
Guest1337

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