concatenate dates with VBA

kendel

Board Regular
Joined
Mar 2, 2010
Messages
133
Hello, I'm using Widows 7 and excel 2007
I have 2 columns of dates, with M/D format, that I want to merge.
This formula works when copied into cells but I get a "Compile error: Expected: end of statement" error msg when I try to put it into a macro.

Sub insert_formula()

Range("C2:C25").Value = "=TEXT(IF($A:$A="","",$A:$A),"m/d")
&" - "&TEXT(IF($B:$B="","",$B:$B),"m/d")"

End Sub

Here's what I'd like to get
col A col B col C
1/7 1/14 1/7 - 1/14

Thanks for your help
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Possibly

Code:
Sub insert_formula()

Range("C2:C25").Formula = "=TEXT(IF($A:$A="""","""",$A:$A),""m/d"") & "" - "" & TEXT(IF($B:$B="""","""",$B:$B),""m/d"")"

End Sub
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
I think this is better (clearer)

Code:
Sub insert_formula()

Range("C2:C25").Formula = "=TEXT(IF(A2="""","""",A2),""m/d"") & "" - "" & TEXT(IF(B2="""","""",B2),""m/d"")"

End Sub
 

kendel

Board Regular
Joined
Mar 2, 2010
Messages
133
Thank you!
I had a suspicion that the quotation marks would be involved
 

Watch MrExcel Video

Forum statistics

Threads
1,129,594
Messages
5,637,299
Members
416,963
Latest member
zazama

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
Top