I'm creating a function that has as a parameter a day of the week (Monday, Wednesday, Thursday ...) that captures the day of the respective month of the week and returns its day in the cell, on Sundays the day value is reset
Function dayReference(week as Integer)
If Weekday(Date, 1) = week Then
ActiveCell.FormulaR1C1 = Day(Date)
ElseIf Weekday(Date, 1) = 1 Then
ActiveCell.FormulaR1C1 = ""
End If
End Function
I'm having 2 problems:
- With a parameter I cannot call the function either manually or through the spreadsheet, without the parameter I can call it manually.
Example: in the cell of my spreadsheet if I type dayReference (7) excel asks to create a macro or select an existing one, as my model is a function and not a macro I can't call it
- My goal is not to have to execute this script manually by the editor, but to have the behavior of native functions like Day and Date, which are dynamic, and I can make the call in any cell
What is the most efficient way to create it using ?
Publicin front of the wordFunction.publicfunction to excel requires a module to run it, and when placing the function inside the module this error is returned: i.imgur.com/smSFeDQ.png,Debug.Printstatements send their output), and then type?dayReference(3)and you'll invoke the function with parameter value3when you hit ENTER, and the?is shorthand forPrintand so after the function returns it should print its return value - but your function here doesn't return anything. Functions shouldn't have side-effects like this.