VBA to insert Min/Max in cell

Calvin_Hobbes

New Member
Joined
Jun 11, 2020
Messages
2
Office Version
  1. 2016
Platform
  1. MacOS
Hi,
I need to insert formulas using VBA and all work but the Min and Max formulas.

This formula when entered directly into a worksheet cell with the formula bar works as it should: =IF(MAX(REPORT!G2:G21)=0, ", MAX(REPORT!G2:G21))

But when entered using VBA it is not recognized as a formula and renders as plain text:
Worksheets("Dashboard").Range("TEST").Formula = " =IF(MAX(REPORT!G2:G21)=0, "", MAX(REPORT!G2:G21))"

formulaResults.jpg


The Min/Max formula is wrapped in the IF statement so that if there is no date present in the indicated range the cell will be empty rather than showing 1900-01-00.

I need to be able to insert the formula(s) using VBA as my worksheet is inserted into another workbook and then posted to Box.
If I insert the sheet with formulas already in place the path to my local workbook is added to the formula(s) and I have to go to each one and delete the reference.

Any help would be appreciated.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Welcome to the Board!

A few things.
Get rid of the space before the equals sign.
You need to double-up on your double-quotes, as they are used as Text Indicators, though you also want literal ones.

So try this:
VBA Code:
Worksheets("Dashboard").Range("TEST").Formula = "=IF(MAX(REPORT!G2:G21)=0,"""",MAX(REPORT!G2:G21))"
 
Upvote 0
You are welcome.
Glad I was able to help!
:)
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,977
Members
452,540
Latest member
haasro02

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