Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 7
Default VBA function to define name in a worksheet

I'm looking for a way to use a VB function to define a named range and alter
the quantity of cells within the named range. I recorded a macro defining a
named range from the "Insert" menu so I could get the VB code. Running a Sub
using this code works fine, but I need to run it by calling a function.
Below is what I've done. The Sub selectRange_SpareCopy achieves the result I
want to get from the function, so I included it as an example.

Function selectRange(rangeName, sheet_RowColumn)
'Called by the selectSheetRange Sub
ActiveWorkbook.Names.Add Name:="rangeName", RefersToR1C1:= _
"=sheet_RowColumn"
End Function

Sub selectSheetRange()
'Give range (A1:A7) on Sheet 1 the name "RangeOne"
Call selectRange("RangeOne", "Sheet1!R1C1:R7C1")
End Sub

Sub selectRange_SpareCopy()

'I've kept this here because this Sub works, and the result I get from this _
is the result I want to achieve with the selectRange function

ActiveWorkbook.Names.Add Name:="RangeOne",
RefersToR1C1:="=Sheet1!R1C1:R7C1"
Range("A1").Select
End Sub

I imagine the main problem will be the lack of
DIM as .... and
Set rangeName As....
but despite all my experimenting I just don't know what to set these up as.
Could anyone help me please?

Thank you
Regards,
Clinton
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 3,440
Default VBA function to define name in a worksheet

Hi Clinton,

A VBA function, called from a worksheet (directly or indirectly) cannot
change anything at all in Excel's worksheet environment. The only thing it
is meant to do and allows you to is to return a value to replace the call to
it.

--
Kind regards,

Niek Otten
Microsoft MVP - Excel

"Clinton W" wrote in message
...
I'm looking for a way to use a VB function to define a named range and
alter
the quantity of cells within the named range. I recorded a macro defining
a
named range from the "Insert" menu so I could get the VB code. Running a
Sub
using this code works fine, but I need to run it by calling a function.
Below is what I've done. The Sub selectRange_SpareCopy achieves the result
I
want to get from the function, so I included it as an example.

Function selectRange(rangeName, sheet_RowColumn)
'Called by the selectSheetRange Sub
ActiveWorkbook.Names.Add Name:="rangeName", RefersToR1C1:= _
"=sheet_RowColumn"
End Function

Sub selectSheetRange()
'Give range (A1:A7) on Sheet 1 the name "RangeOne"
Call selectRange("RangeOne", "Sheet1!R1C1:R7C1")
End Sub

Sub selectRange_SpareCopy()

'I've kept this here because this Sub works, and the result I get from
this _
is the result I want to achieve with the selectRange function

ActiveWorkbook.Names.Add Name:="RangeOne",
RefersToR1C1:="=Sheet1!R1C1:R7C1"
Range("A1").Select
End Sub

I imagine the main problem will be the lack of
DIM as .... and
Set rangeName As....
but despite all my experimenting I just don't know what to set these up
as.
Could anyone help me please?

Thank you
Regards,
Clinton


  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,533
Default VBA function to define name in a worksheet

Hi Clinton

A function can not manipulate a sheet or a reference directly!
Use a (private) sub which can be called from your main sub.

Your variables has been declared, and notice that text in quotation signs
are seen as plain text, so the quotation signs around the variables has been
removed.

Private Sub selectRange(rangeName As String, sheet_RowColumn As String)
'Called by the selectSheetRange Sub
ActiveWorkbook.Names.Add Name:=rangeName, RefersToR1C1:= _
"=" & sheet_RowColumn
End Sub

Sub selectSheetRange()
'Give range (A1:A7) on Sheet 1 the name "RangeOne"
Call selectRange("RangeOne", "Sheet1!R1C1:R7C1")
End Sub

Regards,
Per

"Clinton W" skrev i meddelelsen
...
I'm looking for a way to use a VB function to define a named range and
alter
the quantity of cells within the named range. I recorded a macro defining
a
named range from the "Insert" menu so I could get the VB code. Running a
Sub
using this code works fine, but I need to run it by calling a function.
Below is what I've done. The Sub selectRange_SpareCopy achieves the result
I
want to get from the function, so I included it as an example.

Function selectRange(rangeName, sheet_RowColumn)
'Called by the selectSheetRange Sub
ActiveWorkbook.Names.Add Name:="rangeName", RefersToR1C1:= _
"=sheet_RowColumn"
End Function

Sub selectSheetRange()
'Give range (A1:A7) on Sheet 1 the name "RangeOne"
Call selectRange("RangeOne", "Sheet1!R1C1:R7C1")
End Sub

Sub selectRange_SpareCopy()

'I've kept this here because this Sub works, and the result I get from
this _
is the result I want to achieve with the selectRange function

ActiveWorkbook.Names.Add Name:="RangeOne",
RefersToR1C1:="=Sheet1!R1C1:R7C1"
Range("A1").Select
End Sub

I imagine the main problem will be the lack of
DIM as .... and
Set rangeName As....
but despite all my experimenting I just don't know what to set these up
as.
Could anyone help me please?

Thank you
Regards,
Clinton


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
can I define validation range by function? DPR Excel Worksheet Functions 1 February 2nd 09 09:00 PM
Define Names Embedded in worksheet Karen Excel Discussion (Misc queries) 4 October 20th 08 05:51 PM
User define function lee Excel Discussion (Misc queries) 4 February 11th 07 12:33 AM
How to Define a named Formula at the Worksheet level? RPJ Excel Worksheet Functions 2 October 22nd 06 05:11 PM
How to take the worksheet name as a variable(Label/Define) in a fo Subin Excel Worksheet Functions 2 March 14th 06 12:43 PM


All times are GMT +1. The time now is 01:25 PM.

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

About Us

"It's about Microsoft Excel"