VBA - Struggling with " characters (quotation marks in formula)

velohead

Board Regular
Joined
Aug 22, 2007
Messages
212
Hi All,

Ultimately, I want a piece of code to insert the following excel formula into cell AX2.
="WE-"&YEAR(E2)&"-"&TEXT(E2,"MM")&"-"&TEXT(E2,"DD")


my code at the moment is...
VBA Code:
Sub D207_AX2_Only()
Range("AX2").Select
ActiveCell.Formula = "=""WE-""&YEAR(E2)"
End Sub

the result in excel is ="WE-"&YEAR(E2)
As you can see, the VB code contains more " characters than the result.
This is correct, as I in VB one needs to 'wrap' the 'text bit' in " characters.

I'm struggling to build this up though.
Getting bambozzled with how many " to make it work :(

 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Give this a try:
VBA Code:
"=""WE-""&YEAR(E2)&""-""&TEXT(E2,""MM"")&""-""&TEXT(E2,""DD"")"
 
Upvote 0
Solution
Another option is to include all the formatting in one string instead of many.
VBA Code:
ActiveCell.Formula = "=TEXT(E2,""W\E-yyyy-mm-dd"")"

Also, unless you specifically want to select AX2 first, there is no need to select.
VBA Code:
Sub D207_AX2_Only()
  Range("AX2").Formula = "=TEXT(E2,""W\E-yyyy-mm-dd"")"
End Sub
 
Last edited:
Upvote 0
Another option is to include all the formatting in one string instead of many.
VBA Code:
ActiveCell.Formula = "=TEXT(E2,""W\E-yyyy-mm-dd"")"

Also, unless you specifically want to select AX2 first, there is no need to select.
VBA Code:
Sub D207_AX2_Only()
  Range("AX2").Formula = "=TEXT(E2,""W\E-yyyy-mm-dd"")"
End Sub

Nice, both would streamline my code.
Old habits die hard, I tend to select the cell when I know I can just manipulate it without selecting it. D'oh!
Well suggested, thank you.
 
Upvote 0
You're welcome. :)

I tend to select the cell when I know I can just manipulate it without selecting it.
So long as you are aware & remember that selecting is a (relatively) slow process and if there is a lot of it in your code then the code may run noticeably slower than without the selecting.
 
Upvote 0
You're welcome. :)


So long as you are aware & remember that selecting is a (relatively) slow process and if there is a lot of it in your code then the code may run noticeably slower than without the selecting.
Good point. I was not aware, but perhaps should have been.
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

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