0

I am trying a simple VBA program, that looks like this:

Function NotWorking() As Single

 If Range("C3") = 0 Then
 NotWorking = "=0.6*7"
 Else
 NotWorking = 1.2
 End If

End Function

If cell C3 has 0 as a value, the function NotWorking has to return in the current cell the result of *=0.6*7*, which is 4,2. If the value in C3 is not 0, then the current cell has to return 1,2.

In my case, things aren't the way I expected. In the current cell I type =NotWorking(). If C3 is anything but 0, I obtain 1,2, but when I appropriate 0 to C3, the current cell returns #VALUE.

Why does this code not work?

Best regards and thanks in advance!

2 Answers 2

1

I notice that "=0.6*7" is a string, not a number, but 1.2 is a number.

You have specified that the function returns a single, but "=0.6*7" isn't a single.

Sign up to request clarification or add additional context in comments.

1 Comment

How do you include a cell formula into a VBA?
0

Don't try to return it from the routine, but reach out to the sheet from within the routine.

Sub NotWorking()

   Dim src As Range
   Dim dst As Range

   Set src = Range("C3")
   Set dst = Range("E3")

   If src.Value = 0 Then
       dst.Formula = "=0.6 * 7"
   Else
       dst.Value = 1.2
   End If

End Sub

4 Comments

Very well. This works really nice. Have an upvote. I still need something slightly different - a formula, that could be called on every cell and every sheet of the file. I guess this little video will help me (as well as your code) youtube.com/watch?v=317P-hH0QwM. Greetings. :)
You said "Have an upvote," but the voting stands at zero ... :(
That is, unfortunately, correct. I have rating under 15. Every upvote I give is written down in the database and after (if) I reach rating of 15 or more, the upvotes, I have given, will appear. :)
Ahhh. Thanks for the explanation. So all is NOT vanity!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.