Link to home
Start Free TrialLog in
Avatar of itsmevic
itsmevicFlag for United States of America

asked on

Excel 2010: Date Cell Automation

Hello Experts!

    I'm curious if any one has (or can point me to) a formula or some .vba that I can use that automate the following process:  I'd like it to do an Alt+Enter in ONE cell that contains dates that are vertical of one another and then add the present date (mm/dd/yyyy) at the bottom.  It can be int he form of a button, you click and it does this or as soon as you click into the cell e.g.
 
  Column K
***********************************
* 7/1/2015                                                     *
* 7/2/2015                                                     *
* 7/3/2015                                                     *
* 7/4/2015                                                     *
* 7/5/2015                                                     *
* 7/28/2015 <--present date added          *       <-----ONE Cell, let's call this K2 as an example.
***********************************


Any help is GREATLY appreciated!  Thank you.
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

You can use the SelectionChange event to accomplish this...

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = [A1].Address Then
        Target.Value = Target.Value & vbCrLf & Date
    End If
End Sub

Open in new window


This code will go in the applicable worksheets code module (right-click tab > View Code). It will append the current date to the value already present in cell A1 (modify to suit).
Avatar of itsmevic

ASKER

I went into the code.  I see Sheet 1 which is my Raw Data sheet where I want to place the code at.  I right click on Sheet1 (Raw Data) and select Insert > Module.  It inserts the Module.  I then double-click on the module to go into it.  I then right click and paste the code.   I click Save.   I then click the green arrow to run the macro, It pops open the macro dialog box and wants me to give it a name.  I give it the name "test" it then adds "test" as a sub. e.g.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = [k78].Address Then
        Target.Value = Target.Value & vbCrLf & Date
    End If
End Sub
_____________________________________________________________________________
Sub test()

End Sub

It's been a while since I've messed with this stuff, obviously I'm doing something wrong...
No, it goes into the sheet's module, not a general module as you have done. The easiest way to get to the worksheet code module is to right-click the tab in the main Excel window and select "View Code". Otherwise, while in the VBE, double click the worksheet in the project explorer.
Try this code and then set it to run on keyboard shortcut:
(pop it in a normal module)

Sub addTodayToBottom()

Cells(ActiveCell.End(xlDown).Row + 1, ActiveCell.Column).Value = Date

End Sub

Open in new window

Or this one can be less buggy:

Sub addTodayToBottom()

Cells(Range(Cells(Rows.Count, ActiveCell.Column), Cells(Rows.Count, ActiveCell.Column)).End(xlUp).Row + 1, ActiveCell.Column).Value = Date

End Sub

Open in new window

Steve, you're missing the vital requirement. The date is appended to the value already in the selected cell.
No... the way I read this:

I'd like it to do an Alt+Enter in ONE cell that contains dates that are vertical of one another and then add the present date (mm/dd/yyyy) at the bottom.

Is that the date should be appended to the bottom of the currently selected column.
Not the currently selected cell.
in ONE cell

Note the bolded ONE and the diagram. Plus Alt+Enter inserts a carraige return in a cell.
AH, I stand corrected... I totally mis-understood.

I would tend not to use the 'on change event' as it may add too many dates to the cell:

Sub addTodayToCell()

ActiveCell.Value = Activecell.value & vbCrLf & format(Date,"mm/dd/yyyy")

End Sub

Open in new window

Great feedback, thank you guys.  I'm going to actually end up using both code sets.

Wayne, for your code, I need the macro to encompass each cell on down starting at K78, what would be the proper syntax so that each cell in column K is going to do what the code below says?  I just threw in the k200 as an example.  I'm sure you understand what I"m try to describe.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = [k78; k200].Address Then
        Target.Value = Target.Value & vbCrLf & Date
    End If
End Sub
Steve - Thanks for your input, for your code, let's say I just wanted to do it 'on click' rather than having to use a short cut key?  Is there an OnClick variable we could add to your code and it do the same function as you've programmed it to thus far?  I'm in and out of these cells a lot, to be able to just click on it and bam! it do the alt+enter, add present date to the bottom of that cell would be priceless, at least in my case it would be.
SOLUTION
Avatar of Steve
Steve
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Fantastic!  If I could give you both 1000 points I would!  Unfortunately, we'll have to settle and are limited to a measly 250 points each.  :(

I'm using both macros now as I like the subtle differences in each.  Again, thanks guys!