Fixing date of entry

C

Colin Hayes

Hi all

I have a small problem that I hope someone can help with.

In cell A1 I have this code :

=IF(B1=""," ",TODAY())

With this , if B4 is empty the cell is empty. If B4 has content then it
puts the date.

I replicate this down to A19. As I make entries in the cells in column B
, so the date of entry is recorded.

Unfortunately of course the date of entry is updated to the present date
when I open the workbook. How can I fix the date of entry so that it
remains at the date entry was originally made in column B?

Grateful for any assistance.
 
G

Gord Dibben

I would abandon the use of a formula which uses the function TODAY().

TODAY() is a volatile function so will not remain static.

To get a static date entered you can use VBA sheet event code.

Right-click on the INPUT sheet tab and "View Code"

Copy/paste the following code into that module.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
On Error GoTo enditall
Application.EnableEvents = False
If Target.Cells.Column = 2 Then
n = Target.Row
If Me.Range("B" & n).Value <> "" Then
Me.Range("A" & n).Value = Date
End If
End If
enditall:
Application.EnableEvents = True
End Sub

If you do want to use the volatile TODAY() function, see John McGimpsey's site
for
a method using circular references.

http://www.mcgimpsey.com/excel/timestamp.html

Note also the VBA solution, which is similar to above event code.


Gord Dibben MS Excel MVP
 
C

Colin Hayes

Gord Dibben said:
I would abandon the use of a formula which uses the function TODAY().

TODAY() is a volatile function so will not remain static.

To get a static date entered you can use VBA sheet event code.

Right-click on the INPUT sheet tab and "View Code"

Copy/paste the following code into that module.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
On Error GoTo enditall
Application.EnableEvents = False
If Target.Cells.Column = 2 Then
n = Target.Row
If Me.Range("B" & n).Value <> "" Then
Me.Range("A" & n).Value = Date
End If
End If
enditall:
Application.EnableEvents = True
End Sub


Hi

OK thanks very much for that.

I have more than one sheet in the workbook , and assume I need to enter
the code under each tab. When I enter text into a cell in B1 , the date
appears in the cell A1 , which is perfect.

Out of interest , can the code be modified so that if subsequently the
text entered into B1 is now deleted , that the date in A1 would be
removed too?

Thanks again.
 
G

Gord Dibben

and assume I need to enter
the code under each tab

Incorrect assumption.

When you need the same code in multiple worksheets, you can place the code once
in Thisworkbook module and cover all sheets.

Put this revised code in Thisworkbook Module under Microsoft Excel Objects in
VBE

Private Sub Workbook_SheetChange(ByVal Sh As Object, _
ByVal Target As Range)
On Error GoTo enditall
Application.EnableEvents = False
If Target.Cells.Column = 2 Then
n = Target.Row
With ActiveSheet
If .Range("B" & n).Value <> "" Then
.Range("A" & n).Value = Date
Else: .Range("A" & n).Value = ""
End If
End With
End If
enditall:
Application.EnableEvents = True
End Sub

Delete the previous code you copied to any worksheet module.


Gord
 
C

Colin Hayes

HI

OK thanks for your time and expertise. It's working perfectly.

Best Wishes ,
 
C

Colin Hayes

Gord Dibben said:
Good to hear.

Gord

Hi

Thanks for your help with this.

I see the code works on columns A & B. Would it be an easy thing to
modify it so that it works across the other rows in the worksheet?
Ideally I'm trying to have 12 months worth across.

Effectively this would mean columns A ,C , E , G etc individually
displaying the date when text is entered to B , D , F . H ....

Could the code be amended to fit this condition?

Private Sub Workbook_SheetChange(ByVal Sh As Object, _
ByVal Target As Range)
On Error GoTo enditall
Application.EnableEvents = False
If Target.Cells.Column = 2 Then
n = Target.Row
With ActiveSheet
If .Range("B" & n).Value <> "" Then
.Range("A" & n).Value = Date
Else: .Range("A" & n).Value = ""
End If
End With
End If
enditall:
Application.EnableEvents = True
End Sub


Grateful for any assistance.
 
G

Gord Dibben

I'll have a try later this afternoon when my round of golf is complete.

Unless someone else leaps in before I get back(wink, wink..nudge, nudge)


Gord
 
C

Colin Hayes

Gord Dibben said:
I'll have a try later this afternoon when my round of golf is complete.

Unless someone else leaps in before I get back(wink, wink..nudge, nudge)


Gord

Hi Gord

OK thanks - that would be great.

I was wondering if changes within the same the same row might change all
the cells with earlier data in the row to the same date. Or could they
be protected so as not too interfere with previous entries on the same
row.?


Grateful for your help.
 
G

Gord Dibben

Revised code..........note only 6 columns.........you can fill in the rest<g>

There is probably a better method of defining the range but this works.

Private Sub Workbook_SheetChange(ByVal Sh As Object, _
ByVal Target As Range)
On Error GoTo enditall
Application.EnableEvents = False
If Intersect(Range(Target(1).Address), _
Range("B:B, D:D, F:F, H:H, J:J, L:L")) _
Is Nothing Then GoTo enditall
With Target
If .Value <> "" Then
With .Offset(0, -1)
.Value = Date
.Columns.AutoFit
End With
Else: .Offset(0, -1).Value = ""
End If
End With
enditall:
Application.EnableEvents = True
End Sub


Gord
 
G

Gord Dibben

See my other posting for code.

Previous entries in same row are not affected by new entries.

Only operation that can remove or change a date is to re-enter or delete the
value in the target cell as we coded in second attempt.


Gord
 
C

Colin Hayes

Gord Dibben said:
Revised code..........note only 6 columns.........you can fill in the rest<g>

There is probably a better method of defining the range but this works.

Private Sub Workbook_SheetChange(ByVal Sh As Object, _
ByVal Target As Range)
On Error GoTo enditall
Application.EnableEvents = False
If Intersect(Range(Target(1).Address), _
Range("B:B, D:D, F:F, H:H, J:J, L:L")) _
Is Nothing Then GoTo enditall
With Target
If .Value <> "" Then
With .Offset(0, -1)
.Value = Date
.Columns.AutoFit
End With
Else: .Offset(0, -1).Value = ""
End If
End With
enditall:
Application.EnableEvents = True
End Sub


Gord



Hi Gord

OK thanks again for this. It's working perfectly first time.

Grateful for your time and expertise.

Best Wishes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top