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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,211,748
Messages
6,103,686
Members
447,877
Latest member
OrangeJuice

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