Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default how do I create a UDF (VBA) that returns an array (ctrl+shift+enter)

I need to create a worksheet function that can return an
array i.e. that can
be used with the Ctrl+Shift+Enter.

for example :
- I need to be able to select a range (say A2:A10)
- Enter my formula using the function for example
=myfunction(now(), "week")
- Press Ctrl+****+Enter ,and the function would
automatically fill the cells
A2 to A10 with today's date, the date one week from now,
the date two weeks
from now, etc....

I know this can be done as a macro, as simple worksheet
function, but I
can't find out how to do it using UDFs.

Thanks

Fadi
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default how do I create a UDF (VBA) that returns an array (ctrl+shift+enter)

check this out :
http://www.exceltip.com/st/Return_every_n-
th_item_using_VBA_in_Microsoft_Excel/528.html for the
structure of the function. the embedded logic is similar
to what you have in mind.

-----Original Message-----
I need to create a worksheet function that can return an
array i.e. that can
be used with the Ctrl+Shift+Enter.

for example :
- I need to be able to select a range (say A2:A10)
- Enter my formula using the function for example
=myfunction(now(), "week")
- Press Ctrl+****+Enter ,and the function would
automatically fill the cells
A2 to A10 with today's date, the date one week from now,
the date two weeks
from now, etc....

I know this can be done as a macro, as simple worksheet
function, but I
can't find out how to do it using UDFs.

Thanks

Fadi
.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 575
Default how do I create a UDF (VBA) that returns an array (ctrl+shift+enter)

Fadi,

Is this what you are after?

Public Function WeekCounter(dtStart As Date) As Variant
Dim nItems As Integer
Dim vOutput As Variant
Dim nCounter As Integer
nItems = Application.Caller.Cells.Count
nCounter = 0
ReDim vOutput(0 To nItems)
For nCounter = 0 To nItems - 1
vOutput(nCounter) = CDate(CLng(dtStart) + (nCounter * 7))
Next nCounter
WeekCounter = vOutput
End Function

Robin Hammond
www.enhanceddatasystems.com

"Fadi" wrote in message
...
I need to create a worksheet function that can return an
array i.e. that can
be used with the Ctrl+Shift+Enter.

for example :
- I need to be able to select a range (say A2:A10)
- Enter my formula using the function for example
=myfunction(now(), "week")
- Press Ctrl+****+Enter ,and the function would
automatically fill the cells
A2 to A10 with today's date, the date one week from now,
the date two weeks
from now, etc....

I know this can be done as a macro, as simple worksheet
function, but I
can't find out how to do it using UDFs.

Thanks

Fadi



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default how do I create a UDF (VBA) that returns an array (ctrl+shift+enter)

Hi Fadi,

I think this does what you want

Function myFunction(inDate As Date, inType As String) As Variant
Dim nextDate As Date
Dim i As Long
Dim cCells As Long
Dim tmpArray() As Date

nextDate = inDate
cCells = Selection.Cells.Count
ReDim Preserve tmpArray(0 To cCells)
For i = 1 To cCells
tmpArray(i - 1) = nextDate
Select Case LCase(inType)
Case "day": nextDate = nextDate + 1
Case "week": nextDate = nextDate + 7
Case "month": nextDate = nextDate + 30
Case "year": nextDate = nextDate + 365
End Select
Next i

If Application.Caller.Rows.Count = 1 Then
myFunction = tmpArray
Else
myFunction = Application.Transpose(tmpArray)
End If

End Function


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Fadi" wrote in message
...
I need to create a worksheet function that can return an
array i.e. that can
be used with the Ctrl+Shift+Enter.

for example :
- I need to be able to select a range (say A2:A10)
- Enter my formula using the function for example
=myfunction(now(), "week")
- Press Ctrl+****+Enter ,and the function would
automatically fill the cells
A2 to A10 with today's date, the date one week from now,
the date two weeks
from now, etc....

I know this can be done as a macro, as simple worksheet
function, but I
can't find out how to do it using UDFs.

Thanks

Fadi



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
Ctrl+shift+enter [email protected] Excel Worksheet Functions 9 March 26th 09 11:46 AM
Function of Ctrl + Shift + Enter Kenneth Excel Discussion (Misc queries) 1 November 15th 08 10:20 AM
What is Ctrl + Shift + Enter ? lawson Excel Discussion (Misc queries) 6 June 26th 07 08:17 PM
What does hitting Ctrl + Shift + Enter to enter a formula do??? Help a n00b out. qwopzxnm Excel Worksheet Functions 2 October 20th 05 09:06 PM
ctrl/shift/enter thru VBA Jason Morin[_2_] Excel Programming 7 May 5th 04 05:20 PM


All times are GMT +1. The time now is 03:39 PM.

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

About Us

"It's about Microsoft Excel"