P
Peter Williams
Hi Everyone,
I'm new to VBA, so please pardon if this is a silly question. I have
some named ranges (e.g. DATES) and I'd like to have a function with an
integer argument and a range argument that returns a subrange of the
argument range. For the full code there will be two ranges so that
just what occured on those dates is the returned range. Here are some
examples of the way that I've been trying to do this and some of the
errors that I've been able to see the code making. I'd like to know
if there is a standard way of doing this. I think I can hack up
something with sheet formula, but I would prefer to have a working
function for a little more flexiblity. Don't know if this will get me
flamed, but I am using the OS X version of excel, in case that info is
important.
Option Explicit
'test works as expected
Function test1(arg As Integer, dat As Range) As Range
Set test1 = dat.Offset(arg, 0)
End Function
'test works as expected
Function test2(arg As Integer, dat As Range) As Range
Set test2 = dat.Cells(arg + 1, 1)
End Function
'doesn't work as expected (i.e. doesn't return dat(argarg + 1))
Function test3(arg As Integer, dat As Range) As Range
Set test3 = dat.Range(dat.Cells(arg + 1, 1), dat.Cells(arg + 2, 1))
End Function
'doesn't work as expected (returns something in the column I want, but
rows are messed up
'offset in dat returned is sheet offset where dat is stored from dat
start + the offset that I add
Function test4(arg As Integer, dat As Range) As Range
Set test4 = dat.Range(dat.Cells(arg + 1, 0), dat.Cells(arg + 2, 0))
End Function
Function test5(arg As Integer, dat As Range) As Range
Set test5 = dat(dat.Cells(arg + 1, 1), dat.Cells(arg + 2, 1))
End Function
Anyhelp is appreciated, thanks in advance.
I'm new to VBA, so please pardon if this is a silly question. I have
some named ranges (e.g. DATES) and I'd like to have a function with an
integer argument and a range argument that returns a subrange of the
argument range. For the full code there will be two ranges so that
just what occured on those dates is the returned range. Here are some
examples of the way that I've been trying to do this and some of the
errors that I've been able to see the code making. I'd like to know
if there is a standard way of doing this. I think I can hack up
something with sheet formula, but I would prefer to have a working
function for a little more flexiblity. Don't know if this will get me
flamed, but I am using the OS X version of excel, in case that info is
important.
Option Explicit
'test works as expected
Function test1(arg As Integer, dat As Range) As Range
Set test1 = dat.Offset(arg, 0)
End Function
'test works as expected
Function test2(arg As Integer, dat As Range) As Range
Set test2 = dat.Cells(arg + 1, 1)
End Function
'doesn't work as expected (i.e. doesn't return dat(argarg + 1))
Function test3(arg As Integer, dat As Range) As Range
Set test3 = dat.Range(dat.Cells(arg + 1, 1), dat.Cells(arg + 2, 1))
End Function
'doesn't work as expected (returns something in the column I want, but
rows are messed up
'offset in dat returned is sheet offset where dat is stored from dat
start + the offset that I add
Function test4(arg As Integer, dat As Range) As Range
Set test4 = dat.Range(dat.Cells(arg + 1, 0), dat.Cells(arg + 2, 0))
End Function
Function test5(arg As Integer, dat As Range) As Range
Set test5 = dat(dat.Cells(arg + 1, 1), dat.Cells(arg + 2, 1))
End Function
Anyhelp is appreciated, thanks in advance.