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

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
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,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

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