wildesbare
wildesbare

Reputation: 7

Why is my created function returning an error?

I have created a function which has 4 parameters: SearchDate, StartDate, EndDate, Events. The way I wanted the function to work was if the SearchDate is >= for some start date and =< for some end date then the function pulls the events name. For example, if the search was June 17 and the start/end date was June 15/June 18 then it would pull the event.

However the code doesn't seem to work; when I try to use it gives me a value error. I have posted the code and a table, that the function is based on, below.

Function Calendar_Events(SearchDate As Date, StartColumn As Range, EndColumn As Range, EventsColumn As Range)

Dim x As Long
Dim output As Range
For x = 1 To StartColumn.Cells.CountLarge
    If Int(StartColumn.Cells(x)) <= SearchDate And Int(EndColumn.Cells(x)) >= SearchDate Then
       'in place for the case of more events then rows 
       If y >= 3 Then
           output = output & "........"
           Exit For
       End If
       output = output & Left(EventsColumn.Cells(x), 20) & vbNewLine
       y = y + 1
    End If
Next x

End Function

Table:

Start Date  End Date    Event

1/12/2018   1/19/2018   Software Sale
1/31/2018   1/31/2018   Dinner Party
2/1/2018    2/1/2018    Baby Shower
2/12/2018   2/16/2018   Team Retreat
2/15/2018   2/16/2018   Bank Meetings
2/15/2018   2/15/2018   Lunch Date
2/15/2018   2/15/2018   Dinner Date
3/26/2018   3/29/2018   Vacation
3/28/2018   3/29/2018   Swimming
3/28/2018   3/28/2018   Mountain Biking
3/29/2018   3/29/2018   Put away clothes
3/29/2018   4/4/2018    Cottage 
4/2/2018    4/2/2018    Family Photo
4/2/2018    4/4/2018    Software Sale
4/2/2018    4/6/2018    Hire Nanny
4/6/2018    4/6/2018    Day Off

Upvotes: 0

Views: 54

Answers (1)

JNevill
JNevill

Reputation: 50273

1. In order to return a value from a Function you must set the Function name equal to what you want to return.

So at the end of your code you need:

Calendar_Events = output

So it knows to return the output variable you've been building.

2. Furthermore your output variable should be String. You are not collecting Ranges here, but rather the values inside of cells that match your criteria, so:

Dim Output As String

3. Also, there is no need to convert the cell values containing dates to integers. You are comparing dates to dates and that is good to go without converting. so:

If StartColumn.Cells(x).Value <= SearchDate And EndColumn.Cells(x).Value >= SearchDate Then

I've also added .value to the end of the Cell() reference. It will default to the .value property of the cell, but I'm a big fan of explicit coding instead of just hoping the compiler will know which property you meant.

4. Lastly (and optionally) you should declare the TYPE of the return from the function in the function definition. so:

Function Calendar_Events(SearchDate As Date, StartColumn As Range, EndColumn As Range, EventsColumn As Range) As String

All of this together:

Function Calendar_Events(SearchDate As Date, StartColumn As Range, EndColumn As Range, EventsColumn As Range) As String

    Dim x As Long
    Dim output As String
    For x = 1 To StartColumn.Cells.CountLarge
        Debug.Print StartColumn.Cells(x).Value, EndColumn.Cells(x).Value
        If StartColumn.Cells(x).Value <= SearchDate And EndColumn.Cells(x).Value >= SearchDate Then
           'in place for the case of more events then rows
           If y >= 3 Then
               output = output & "........"
               Exit For
           End If
           output = output & Left(EventsColumn.Cells(x), 20) & vbNewLine
           y = y + 1
        End If
    Next x
    Calendar_Events = output
End Function

Upvotes: 1

Related Questions