Converting sheet formular which incudes operators into VBA .formula

Upex

Board Regular
Joined
Dec 29, 2010
Messages
194
Office Version
  1. 365
Platform
  1. Windows
Hi All,

For the life of me I can't get this working, despite throwing quotes all over it.

The sheet has a formula in it at present:

=IF(COUNTIF($H$5:$H$50000,"<"&">"&"")=0,"",COUNTIF($H$5:$H$50000,"<"&">"&""))

Which simply counts the nonblank cells (likely not the best way but hey, it works) with the range of column H.

I want to delete the formula and put the formula into a code module, so I can have it add the formula in, calculate, then copy the result as the cell value (i.e. so the formula is not visible on the sheet, just the result.

I'll not post all the code, as everything is working (except for the insertion of the operators) and its large, but I've tried entering the above sheet formula directly, i.e.
Code:
.Range("i3").Formula = "=IF(COUNTIF($H$5:$H$50000,"<"&">"&"")=0,"",COUNTIF($H$5:$H$50000,"<"&">"&""))"

Had to double up the "" 's, to:
Code:
.Range("i3").Formula = "=IF(COUNTIF($H$5:$H$50000,"<"&">" &"""")=0,"""",COUNTIF($H$5:$H$50000,"<"&">" &""""))"

Tried quoting the operators as well;
Code:
.Range("i3").Formula = "=IF(COUNTIF($H$5:$H$50000,"""<"""&""">""" &"""")=0,"""",COUNTIF($H$5:$H$50000,"""<"""&""">""" &""""))"

Then tried 'unquoting' the operators:
Code:
.Range("i3").Formula = "=IF(COUNTIF($H$5:$H$50000,<> &"""")=0,"""",COUNTIF($H$5:$H$50000,<> &""""))"

Can't get it to work. Any ideas on how operators need to be handled when being used within .formula ?

Thanks all, ​Upex
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Sussed it with alittle help from the macro recorder:

Code:
Range("i3").Formula = "=IF(COUNTIF($H$5:$H$50000,""<""&"">""&"""")=0,"""",COUNTIF($H$5:$H$50000,""<""&"">""&""""))"

Far too many """'s
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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