[VBA] Capture a value before a cell converts it into date format

Snypa

New Member
Joined
Nov 1, 2013
Messages
45
Hello,

I want to write some VBA that does the following:

user enters an integer into a cell
store the integer
change the cell value to '=today()-integer'

I have pretty much got everything I need written, the problem is, I am using Target.Value, which is showing the integer converted into date format. I.e: 5 becomes 04/01/1900, 100 becomes 09/04/1900 etc.

Here is my code:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range

    ' Cells
    Set KeyCells = Range("B4:F9")
    
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then

        ' Code here
        If IsNumeric(Target.Value) Then
            Range(Target.Address).Select
            ActiveCell.FormulaR1C1 = "=today()-" & Target.Value
        End If
     
    End If
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Lets forget about this script and tell me what your wanting to do.


Thank you,

I have a group of cells that have date values in them. Each morning I must work out the oldest of a set of data and enter it into the cells.

So basically, I am writing =today()-x in a bunch of cells.

I want to only have to type x into the cells and have the cell change it to =today()-x

Does that make sense?
 
Upvote 0
Hi,

maybe is better to use vba only

Code:
target.value = cdbl(date) + target.value

regards
 
Upvote 0
I have pretty much got everything I need written, the problem is, I am using Target.Value, which is showing the integer converted into date format. I.e: 5 becomes 04/01/1900, 100 becomes 09/04/1900 etc.

Hi, try like this instead.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    ' Cells
    Set KeyCells = Range("B4:F9")
    
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then
        ' Code here
        If IsNumeric(Target.Value2) Then
            Application.EnableEvents = False
            Target.Formula = "=today()-" & Target.Value2
            Application.EnableEvents = True
        End If
     
    End If
End Sub
 
Last edited:
Upvote 0
Hi, try like this instead.

In fact - this is probably better.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim R As Range, C As Range
Set R = Application.Intersect(Range("B4:F9"), Target)
If Not R Is Nothing Then
    For Each C In R
        If IsNumeric(C.Value2) And C.Value2 <> "" Then
            Application.EnableEvents = False
            C.Formula = "=TODAY()-" & C.Value2
            Application.EnableEvents = True
        End If
    Next C
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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