Adding date to selected cells (VBA)

torkunc

Board Regular
Joined
Jun 26, 2014
Messages
81
Hi,

In VBA, I'd like to add date from A1 to my selected cells.
I can do it if only 1 cell is selected; but when it comes to a range like "a2:a5" I can not!

And there is one condition:
If selected cell(s) is empty then copy the date from A1

but if not

then join the dates of the selected cells and A1 with the delimiter comma (,)​

I think a loop to an array needs to be written but I fail!

Thnx fr yr help.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Something like this does the trick. Change the format to how you want the date:

Code:
Sub IncludeDate()
    Dim c As Range
    Const sFormat As String = "dd/mm/yy"
    
        For Each c In Selection
            c = IIf(c = "", Format([A1], sFormat), c & "," & Format([A1], sFormat))
        Next c
End Sub
 
Upvote 0
gallen - Don't think you meant to put two "I"s in your If statement:
Rich (BB code):
Sub IncludeDate()
    Dim c As Range
    Const sFormat As String = "dd/mm/yy"
    
        For Each c In Selection
            c = IIf(c = "", Format([A1], sFormat), c & "," & Format([A1], sFormat))
        Next c
End Sub
...
...probably should be:
Rich (BB code):
Sub IncludeDate()
    Dim c As Range
    Const sFormat As String = "dd/mm/yy"
    
        For Each c In Selection
            c = If(c = "", Format([A1], sFormat), c & "," & Format([A1], sFormat))
        Next c
End Sub
 
Last edited:
Upvote 0
@sykes
IIf with 2 "I"s is correct.
If you try removing one of them you will see that line of code go red.
 
Last edited:
Upvote 0
Thanks very much Fluff - I've learned something (else) there.
gallen - apologies.
torkunc - disregard my post, please.
 
Upvote 0
No problem @sykes.

The IIF has is handy when reacting to a boolean value and keeps the code all on one line.
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,428
Members
448,896
Latest member
MadMarty

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