Formula is not working in VBA

Kurt

Well-known Member
Joined
Jul 23, 2002
Messages
1,664
I remind the double quotes this time on this formula:

Code:
    Range("A2").Formula = "= IF(A2=0, ""Base Bid"", ""Alternate - "" & A2)"

The code will not function as it is.

Can anyone clue me in on what I need to change?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
You're putting a formula in A2 which refers to A2, or in other words, a circular reference.
 
Upvote 0
What are you trying to do?
Putting that formula in A2 will just give you a circular reference.
 
Upvote 0
Hello,

Fluff and Eric you are both correct I am getting a circular reference.

Does this need to be in R1C1 annotation?

If so can you show me how to correct it?
 
Upvote 0
What are you trying to do?
 
Upvote 0
R1C1 notation won't change anything. The way to correct this is by putting the formula in another cell. Are you trying to insert this formula into a range? If so, something like:

Code:
Range("B2:B10").Formula = "= IF(A2=0, ""Base Bid"", ""Alternate - "" & A2)"
and Excel will handle the relative referencing.
 
Upvote 0
Hi there. You are creating whats called a circular reference - the line of code you have written will put that formula into A2, so A2 will be trying to calculate a result based on itself. If you want the result as a formula in another cell, change the first A2 to where you want it to be. If what you are doing is trying to look at the contents of A2 and replace them based on what is in there, then try this:

Code:
If Range("A2").Value = 0 Then
    Range("A2").Value = "Base Bid"
Else
    Range("a2").Value = "Alternate - " & Range("A2").Value
End If
 
Upvote 0
Not sure i understand what you are trying to do.

See if this is ok (guessing...)
Code:
Range("A2") = Evaluate("=IF(A2=0,""Base Bid"",""Alternate - ""&A2)")

Beware that this overwrites the value in A2

M.
 
Upvote 0
Hi there. You are creating whats called a circular reference - the line of code you have written will put that formula into A2, so A2 will be trying to calculate a result based on itself. If you want the result as a formula in another cell, change the first A2 to where you want it to be. If what you are doing is trying to look at the contents of A2 and replace them based on what is in there, then try this:

Code:
If Range("A2").Value = 0 Then
    Range("A2").Value = "Base Bid"
Else
    Range("a2").Value = "Alternate - " & Range("A2").Value
End If

Hello jmaleary,

You are exactly correct!

Thanks for everyone and their input.

This worked beautifully!

However it is still not accounting for the Alternate piece when the value is a 1 instead of a 0.

We are almost there!

Cheers from across the pond!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,124
Messages
6,123,189
Members
449,090
Latest member
bes000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top