VBA formula

Aqusmacro

New Member
Joined
Jan 8, 2024
Messages
46
Office Version
  1. 2013
Platform
  1. Windows
Hi trying to paste a formula using VBA
The first line is a dummy line to test and it works
but the second and 3rd which I want to use are giving my runtime error 1004

When pasting the formula directly in the worksheet it works

Reason I want to use through VBA is that I need to clear part of the worksheet before importing new data and that will give me a REF error in the formula in the worksheet
therefore want to add after import


VBA Code:
WS.Range("D2").Formula = "=i2+j2"
WS.Range("F2:F100").Formula = "=IFNA(INDEX('Data Validation'!$O$3:$O$40,MATCH($D2,'Data Validation'!$N$3:$N$40,0)),"")"
WS.Range("G2:G100").Formula = "=IFNA(INDEX('Data Validation'!$P$3:$P$40,MATCH($D2,'Data Validation'!$N$3:$N$40,0)),"")"
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Try these variants:
VBA Code:
WS.Range("F2:F100").Formula = "=IFNA(INDEX('Data Validation'!$O$3:$O$40,MATCH($D2,'Data Validation'!$N$3:$N$40,0)),"""")"
WS.Range("G2:G100").Formula = "=IFNA(INDEX('Data Validation'!$P$3:$P$40,MATCH($D2,'Data Validation'!$N$3:$N$40,0)),"""")"
 
Upvote 0
Solution
Try these variants:
VBA Code:
WS.Range("F2:F100").Formula = "=IFNA(INDEX('Data Validation'!$O$3:$O$40,MATCH($D2,'Data Validation'!$N$3:$N$40,0)),"""")"
WS.Range("G2:G100").Formula = "=IFNA(INDEX('Data Validation'!$P$3:$P$40,MATCH($D2,'Data Validation'!$N$3:$N$40,0)),"""")"
that worked perfecty. Thanks
Just by adding extra quotes...
 
Upvote 0
Thank you for the feedback
Doublequotes (") are used to start and terminate a string; to insert a doublequote in a string you need to insert ""; thus to simulate a "null string" you need to use """"
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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