Solved

Simple Excel 2010 issue

Posted on 2013-10-30
7
219 Views
Last Modified: 2013-10-30
This seems simple enough... This following code is attached to a button to clear some data on a spreadsheet...

    Range("H3").Select
    ActiveCell.FormulaR1C1 = "=DATE(YEAR(F3),MONTH(F3)+1,DAY(F3))-1"

The formula works fine but when it runs in code, Excel replaces the cell with this...
=DATE(YEAR('F3'),MONTH('F3')+1,DAY('F3'))-1

(Note the single quotes that are added)

If I edit the formula and remove the single quotes it works as expected but Excel is adding the single quotes when it inserts the formula.

How can I have Excel insert the code as I have written it??

Thx!
0
Comment
Question by:DanielT
  • 3
  • 3
7 Comments
 
LVL 12

Expert Comment

by:Harry Lee
ID: 39612923
Daniel,


ActiveCell.FormulaR1C1 = "=DATE(YEAR(R3C6),MONTH(R3C6)+1,DAY(R3C6))-1"
0
 
LVL 50

Assisted Solution

by:barry houdini
barry houdini earned 100 total points
ID: 39612924
You are using "FormulaR1C1" but the notation in the formula is using conventional cell references, for R1C1 you need to make the references relative to H3 so because F3 is 2 columns before H3 that would be

ActiveCell.FormulaR1C1 = "=DATE(YEAR(RC[-2]),MONTH(RC[-2])+1,DAY(RC[-2]))-1"

regards, barry
0
 
LVL 12

Accepted Solution

by:
Harry Lee earned 200 total points
ID: 39612930
Or you can use

 ActiveCell.Formula = "=DATE(YEAR(F3),MONTH(F3)+1,DAY(F3))-1"
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:DanielT
ID: 39612943
Thank you both.
I see that both methods work OK.

Is this simply because the "FormulaR1C1" property cannot accept conventional cell references?
0
 
LVL 2

Author Comment

by:DanielT
ID: 39612954
I like the last example best. Thx HarryHYLee!

I think I started this code with an Excel Macro - could have been migrated from Excel 2003 as well.
0
 
LVL 12

Assisted Solution

by:Harry Lee
Harry Lee earned 200 total points
ID: 39612981
Daniel,

Yes, the difference between those two are cell reference format.

Activecell.Formula uses the A1 cell reference format, and
Activecell.FormulaR1C1 uses the R1C1 format. R? is the row #, and C? is the column #.

They are not compatible with each other, and will not accept the wrong type of cell references.
0
 
LVL 2

Author Comment

by:DanielT
ID: 39612986
Thanks again!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
My experience with Windows 10 over a one year period and suggestions for smooth operation
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now