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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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