I want to write a Function Macro that calculates the days between two dates. It looks like I have that.
Function daysRem(today As Date, eoc As Date) As Integer
daysRem = Abs(DateDiff("d", today, eoc))
End Function
Now, I need to "Call" that function from a Sub to roughly estimate the number of weeks remaining. I'd like that to be in a message box. That's where I've hit about 2 hours of frustration. This has got to be simple, but I just can't figure out what to do.
Try this:
Private Sub DisplayDaysDiff()
Dim dtDate1 as Date
Dim dtDate2 as Date
dtDate1 =ThisWorkbook.Worksheets("Sheet1").Range("A2")
dtDate2=ThisWorkbook.Worksheets("Sheet1").Range("B2")
MsgBox "Days difference: " & CStr(daysRem(dtDate1, dtDate2)), vbInformation
End Sub
Call it from a button_click event
Sub Main
x = MyFunction(Param1)
End Sub
MyFunction(MyDate as Date)
MyFunction = DateDiff("ww", Date(), MyDate)
End Function
You assign the value to the function name. The inbuilt Date()
function is todays date.
Functions return something. Subs don't. Macros ARE SUBS.
Functions return something.
So
Function Test
Test = 5
End Function
Sub Main
x = Test() + 5
End Sub
So x = 10 .
Sub Main
Param1 = #2/2/2017#
Range("B4").Value = MyFunction(Param1)
End Sub
MyFunction(MyDate as Date)
MyFunction = DateDiff("ww", Date(), MyDate)
End Function
Date is both a data type and a function. One doesn't have brackets and one does.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.