Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
hjc hjc is offline
external usenet poster
 
Posts: 17
Default named range in macro call

Is there a way to pass a named range (or even an unnamed block of cells) into
a macro?

I have written a macro that I want to use for several different ranges of
data. I have created multiple buttons that each call the macro, but I cannot
seem to find the right syntax to pass in the range of data that I want the
macro to use for each button. All I can do is to pass in the row and column
numbers that define the upper left and lower right corners of the data range,
as follows:

Private Sub CommandButton1_Click()
' Call Macro1(UpperLeftRow, UpperLeftCol, LowerRightRow, LowerRightCol)
Call Macro1(16, 9, 40, 35)
End Sub

Private Sub CommandButton2_Click()
Call Macro1(51, 9, 75, 35)
End Sub

etc.

If possible, I would like to be able to specify named ranges instead, so
that I don't have to edit the code for all the buttons if I add, delete or
move rows or columns. Is there a way to do this?

Thanks!
hjc

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default named range in macro call

try something like this...

public sub macro1(rng as range)
msgbox rng.address
end sub

Sub test
Call macro1(thisworkbook.names("MyRange").referstorange )
end sub
--
HTH...

Jim Thomlinson


"hjc" wrote:

Is there a way to pass a named range (or even an unnamed block of cells) into
a macro?

I have written a macro that I want to use for several different ranges of
data. I have created multiple buttons that each call the macro, but I cannot
seem to find the right syntax to pass in the range of data that I want the
macro to use for each button. All I can do is to pass in the row and column
numbers that define the upper left and lower right corners of the data range,
as follows:

Private Sub CommandButton1_Click()
' Call Macro1(UpperLeftRow, UpperLeftCol, LowerRightRow, LowerRightCol)
Call Macro1(16, 9, 40, 35)
End Sub

Private Sub CommandButton2_Click()
Call Macro1(51, 9, 75, 35)
End Sub

etc.

If possible, I would like to be able to specify named ranges instead, so
that I don't have to edit the code for all the buttons if I add, delete or
move rows or columns. Is there a way to do this?

Thanks!
hjc

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default named range in macro call

Sub Macro1(myRng as range)
msgbox myrng.address(external:=true)
End Sub

And you can call it like:

Dim Rng as range
set rng = workbooks("Some workbook.xls").worksheets("sheet99").range("a1:b9" )
Call macro1(myRng:=rng)






hjc wrote:

Is there a way to pass a named range (or even an unnamed block of cells) into
a macro?

I have written a macro that I want to use for several different ranges of
data. I have created multiple buttons that each call the macro, but I cannot
seem to find the right syntax to pass in the range of data that I want the
macro to use for each button. All I can do is to pass in the row and column
numbers that define the upper left and lower right corners of the data range,
as follows:

Private Sub CommandButton1_Click()
' Call Macro1(UpperLeftRow, UpperLeftCol, LowerRightRow, LowerRightCol)
Call Macro1(16, 9, 40, 35)
End Sub

Private Sub CommandButton2_Click()
Call Macro1(51, 9, 75, 35)
End Sub

etc.

If possible, I would like to be able to specify named ranges instead, so
that I don't have to edit the code for all the buttons if I add, delete or
move rows or columns. Is there a way to do this?

Thanks!
hjc


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,203
Default named range in macro call

This might also help along with what Jim Thomlinson offered.

This code snippet can be used to come up with the row and column numbers for
the upper left corner and lower right corner of a named range, just
substitute the actual name of the range where I show "NamedRangeName" in the
snippet


Dim UpperLeftRow As Long
Dim UpperLeftCol As Long
Dim LowerRightRow As Long
Dim LowerRightCol As Long
Dim rangeAddress As String

rangeAddress = ThisWorkbook.Names("NamedRangeName").RefersToR1C1
rangeAddress = Right(rangeAddress, Len(rangeAddress) - _
InStr(rangeAddress, "!"))
UpperLeftRow = Val(Mid(rangeAddress, 2, _
InStr(rangeAddress, "C") - 2))
UpperLeftCol = Val(Mid(rangeAddress, _
InStr(rangeAddress, "C") + 1, InStr(rangeAddress, ":") - _
InStr(rangeAddress, "C") + 1))
rangeAddress = Right(rangeAddress, Len(rangeAddress) - _
InStr(rangeAddress, ":"))
LowerRightRow = Val(Mid(rangeAddress, 2, _
InStr(rangeAddress, "C") - 2))
LowerRightCol = Val(Right(rangeAddress, _
Len(rangeAddress) - InStr(rangeAddress, "C")))
'then you could call your macro as:
Call Macro1(UpperLeftRow, UpperLeftCol, LowerRightRow, LowerRightCol)


"hjc" wrote:

Is there a way to pass a named range (or even an unnamed block of cells) into
a macro?

I have written a macro that I want to use for several different ranges of
data. I have created multiple buttons that each call the macro, but I cannot
seem to find the right syntax to pass in the range of data that I want the
macro to use for each button. All I can do is to pass in the row and column
numbers that define the upper left and lower right corners of the data range,
as follows:

Private Sub CommandButton1_Click()
' Call Macro1(UpperLeftRow, UpperLeftCol, LowerRightRow, LowerRightCol)
Call Macro1(16, 9, 40, 35)
End Sub

Private Sub CommandButton2_Click()
Call Macro1(51, 9, 75, 35)
End Sub

etc.

If possible, I would like to be able to specify named ranges instead, so
that I don't have to edit the code for all the buttons if I add, delete or
move rows or columns. Is there a way to do this?

Thanks!
hjc

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
named range in macro call hjc Excel Programming 3 April 1st 10 08:59 PM
Named Range Macro Lost Excel Discussion (Misc queries) 1 September 22nd 09 11:17 PM
Trying to call a macro on selection of a cell in a range The Narcissist Excel Programming 2 September 26th 08 10:25 PM
Call Macro when Cell within Range Changes Andibevan[_2_] Excel Programming 4 March 24th 05 04:28 PM
Passing a range in a macro call Otto Moehrbach[_6_] Excel Programming 6 November 15th 04 02:31 PM


All times are GMT +1. The time now is 03:20 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"