Custom Excel VBA Macro Function with return type as Date

  • Hi,


    With THANKING YOU in advance,

    I'm running into some troubles in writing this Macro Function in Excel VB. I have a workbook with multiple sheets. Except for the Summary Sheet, all other sheets have a dynamic range of consecutive cells (within a column) that stores some Date formatted values. I need to select that range dynamically and choose the Largest Date Value. The Function should return that value.Here's what I was trying:


    Function GetMSDate(Counter As Integer, DesignCtrs() As String, NumOfVendors() As Long, MSDesc() As String, PlanOrActual As String) As Date GetMSDate = Application.Max(ThisWorkbook.Worksheets(Counter + 1).Range("A:A") .Find(PlanOrActual, SearchOrder:=xlByColumns, searchDirection:=xlNext) .EntireRow .Find(MSDesc(1), SearchOrder:=xlByColumns, searchDirection:=xlNext) .Offset(1, 0).Address & ":" & _ ThisWorkbook.Worksheets(Counter + 1).Range("A:A") .Find(PlanOrActual, SearchOrder:=xlByColumns, searchDirection:=xlNext) .EntireRow .Find(MSDesc(1), SearchOrder:=xlByColumns, searchDirection:=xlNext) .Offset(NumOfVendors(Counter) + 1, 0).Address) MsgBox GetMSDateEnd Function

    My intent was that I would define the dynamically constructed Range for the Application.Max()argument. In doing so, finding the range anchor, I choose the correct sheet (Counter+1 allows that correctly); then find a specific "predefined" string in the first column; select the entire row of the found cell and find within that range another predefined string; the range is then defined from the cell below that anchor cell to an offset defined by: NumOfVendors(Counter)+1In running the above, it gives me:

    [INDENT]Type Mismatch error at the "GetMSDate = " statement.


    [/INDENT]

  • Re: Custom Excel VBA Macro Function with return type as Date


    Quote from BenC1985;653128

    Could you please put you code in Code Tags.


    My bad. just resubmitting the function wrapped within the tag. Thank you


    Code
    [COLOR=#00008B]Function[/COLOR][COLOR=#333333] GetMSDate(Counter [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#333333] [/COLOR][COLOR=#00008B]Integer[/COLOR][COLOR=#333333], DesignCtrs() [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#333333] [/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#333333], NumOfVendors() [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#333333] [/COLOR][COLOR=#00008B]Long[/COLOR][COLOR=#333333], MSDesc() [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#333333] [/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#333333], PlanOrActual [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#333333] [/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#333333]) [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#333333] [/COLOR][COLOR=#00008B]Date[/COLOR][COLOR=#333333] GetMSDate = Application.Max(ThisWorkbook.Worksheets(Counter + [/COLOR][COLOR=#800000]1[/COLOR][COLOR=#333333]).Range([/COLOR][COLOR=#800000]"A:A"[/COLOR][COLOR=#333333]) .Find(PlanOrActual, SearchOrder:=xlByColumns, searchDirection:=xlNext) .EntireRow .Find(MSDesc([/COLOR][COLOR=#800000]1[/COLOR][COLOR=#333333]), SearchOrder:=xlByColumns, searchDirection:=xlNext) .Offset([/COLOR][COLOR=#800000]1[/COLOR][COLOR=#333333], [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#333333]).Address & [/COLOR][COLOR=#800000]":"[/COLOR][COLOR=#333333] & _ ThisWorkbook.Worksheets(Counter + [/COLOR][COLOR=#800000]1[/COLOR][COLOR=#333333]).Range([/COLOR][COLOR=#800000]"A:A"[/COLOR][COLOR=#333333]) .Find(PlanOrActual, SearchOrder:=xlByColumns, searchDirection:=xlNext) .EntireRow .Find(MSDesc([/COLOR][COLOR=#800000]1[/COLOR][COLOR=#333333]), SearchOrder:=xlByColumns, searchDirection:=xlNext) .Offset(NumOfVendors(Counter) + [/COLOR][COLOR=#800000]1[/COLOR][COLOR=#333333], [/COLOR][COLOR=#800000]0[/COLOR][COLOR=#333333]).Address) MsgBox GetMSDate[/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#333333] [/COLOR][COLOR=#00008B]Function[/COLOR]
  • Re: Custom Excel VBA Macro Function with return type as Date


    Please delete all of the code from both of your posts, then repost the code using *ONLY* code tags.
    Never use any other formatting with code. The code tags override all other tags.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!