TEXT formula help

skull_eagle

Board Regular
Joined
Mar 25, 2011
Messages
89
Hi,

I've written the below formula and it work fine, It works out the Min and max years in a range eg. 1998 - 2000

The problem is when the min and max are the same I end up with 1998 - 1998

Is there any way I can make an adjustment to just show the 1 year when this occurs.

Code:
    ActiveCell.FormulaR1C1 = _
        "=TEXT(MIN(R[1]C:R[20]C), ""yyyy"") & "" - "" & TEXT(MAX(R[1]C:R[20]C), ""yyyy"")"


Thanks
 

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.
Why use TEXT and not just YEAR? Makes it somewhat shorter, assuming of course the range you use contains dates!

Try this:
Code:
ActiveCell.FormulaR1C1 = _
"=IF(YEAR(MIN(R[1]C:R[20]C))=YEAR(MAX(R[1]C:R[20]C));YEAR(MIN(R[1]C:R[20]C)); YEAR(MIN(R[1]C:R[20]C)) & "" - "" & YEAR(MAX(R[1]C:R[20]C)))"
 
Upvote 0
Thanks for the response.

Unfortunately I can't get that code to work, I just keep getting a 'Run-Time error 1004' error.

Any ideas?


Thanks
 
Upvote 0
Hmm, it fails for me as well...

I had tested the formula itself on a dummy range, then converted to VBA and your RC-style ranges...

For what range are you doing this?
Why are you using RC-style reference?
 
Upvote 0
I need the formula to run in cell B1 each time, I have no preference for style just as long as it works :)


Thanks
 
Upvote 0
Found the reason: replace the semicolons with commas and it works :biggrin:

Excel is messy in that regard, imho, depending on the language of your version and if you're working on a worksheet or in VBA, you need a comma or a semicolon, or viceversa, and I never seem to get it right the first time :(
 
Upvote 0
you're welcome...

the irony of the ,; issue is that the actual formula you end up with in the cell, again has semicolons, even though the string in VBA needs commas for it to work... go figure :biggrin:
 
Upvote 0
Hi

Just to clarify.

The properties .Formula and .FormulaR1C1 should be used with formulas with the English syntax.

You can, however, use formulas according to your regional settings. In that case, use instead the properties .FormulaLocal and .FormulaR1C1Local
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,726
Members
452,939
Latest member
WCrawford

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