Assign formula to a cell using VBA

thecat23

New Member
Joined
Apr 6, 2019
Messages
19
Office Version
  1. 365
Hi all.

I am trying to assign a formula to a cell, which is a if function that will assign values based on if the cell is empty or not.

The sub reset_works() is using Formula2R1C1, this one works.

However, I couldn't figure out why when I am switching to Formula, which is the sub reset_notworking(), it pumps a run-time error '1004'?

Thanks all.

1676685561298.png


VBA Code:
 Sub reset_works()
 
 Range("D2").Formula2R1C1 = "=IF(RC[-3]<>"""",R[-1]C[-2],"""")"
 Range("D3").Formula2R1C1 = "=IF(RC[-3]<>"""",R[-2]C[-2],"""")"
 
 End Sub
 
 Sub reset_notworking()
 
 ActiveSheet.Range("D2").Formula = "=IF(A2<>"";B1;"")"
 
 End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hello TheCat,

You still need to double up on the quotation marks and replace the semi colons with commas.

ActiveSheet.Range("D2").Formula = "=IF(A2<>"""",B1,"""")"

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Hello TheCat,

You still need to double up on the quotation marks and replace the semi colons with commas.

ActiveSheet.Range("D2").Formula = "=IF(A2<>"""",B1,"""")"

I hope that this helps.

Cheerio,
vcoolio.
Hi Voolio,

Works like a charm!

Appreciated it!

Regards
 
Upvote 0
You're welcome . I'm glad to have been able to assist.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,215,025
Messages
6,122,732
Members
449,093
Latest member
Mnur

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