February 12th, 2012, 3:27 pm
QuoteOriginally posted by: playerA ridiculously simple question but since I havent used vb for a long time Im gonig mad at this.I have written this function Function output(i As Double, j As Double)If i > j ThenWith Range("A1").Offset(1, 1) = 3End WithEnd IfHowever the program crashes at the line .Offset(1,1)=3When I write it as a subrountine it works but as a function it crashes. What am I doing wrong? In particular how can I output a value in excel from the vb editor in a function? eg even this does not work Function output(i As Double, j As Double)If i > j ThenRange("A1") = 5End IfEnd FunctionI am late as usual, but still wanted to comment this. VBA functions called from worksheet have some limitations. For example, they can not change any cells, apply formatting, open workbooks, etc. If the function attemps to do any of these things, the formula return an error. This is the reason, why your function crashes at that line. So, VBA functions called from worksheet can only perform calculations and return value(s). One exception to this rule is VBA MsgBox ..
Last edited by
MikeJuniperhill on February 11th, 2012, 11:00 pm, edited 1 time in total.