VBA Date String Issue

playern07

New Member
Joined
Feb 7, 2011
Messages
22
I have a string of dates I am trying to concatenate. I would like to take a date and put it in a particular format, but for some reason my code only puts the 1st date in such format and the rest of them in the arbitrary formatt. Any ideas?

Sheet1.Select
Dim DateQRY As String
Dim cell2 As Range
DateQRY = Sheet1.Range("$B$2")
DateQRY = Format(DateQRY, "'yyyy-mm-dd'")
For Each cell1 In Range("OFFSET($B$3,0,0,COUNT($B:$B)-1,1)").Cells
DateQRY = DateQRY & "," & cell1.Value
Next

Sample Output: DateQRY = ('2011-07-07',7/0/2010,7/23/2011,...)
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try

Code:
Sub DateTest()
Dim LR As Long, i As Long, s As String
LR = Range("B" & Rows.Count).End(xlUp).Row
For i = 2 To LR
    s = s & Format(Range("B" & i).Value, "yyyy-mm-dd") & ", "
Next i
Range("C1").Value = Left(s, Len(s) - 2)
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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