If a cell contains a certain "word", then return todays date

DarkoDeign2

Board Regular
Joined
Jun 20, 2023
Messages
76
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have been searching the web for a workable formula, but so far I haven't been able to find anything to make use of.

Cell AE2 can be blank, or contain one of three possible words: "Apple", "Car" or "Book".
In cell AG2 I would like to have a formula that returns "todays date" if cell AE2 contains the word "Apple". Furthermore I would like to have the date static, so it doesn't change over time.

Would appreciate if somebody could help me figuring this out. :)
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
That is best done with VBA, as Excel formulas to return the current date use the TODAY() function, which is dynamic, which means it changes every day.
So it is best to use VBA to hard-code the current date so it doesn't change.

However, there are two ways to go about it:
1. If you want this to run on existing data in column AE, you would want to manually run the VBA code upon demand.
2. If you want, you could have VBA code that runs automatically as you enter data into column AE. This obviously only works for new data entry, not existing data.

Also, should we assume that you want this to run for all cells below AE2 (i.e. AE3, AE4, AE5, etc), and not just cell AE2?

Just let us know what option works for your situation, and we can help you write that VBA code.
 
Upvote 0
Hi Joe,

Thanks for your help.
The best alternative would be option 2, a running VBA code for any new inputs into column AE.
Yes there will be data in cell AE2 and below. Since I don't know how easy it is to have a code that scans cell AE2:AE1500? Will the file become heavy and sluggish if I have VBA codes running on 1500 lines? In this file I have multiple conditional formatting formulas running already, so far so good, no sluggishness yet :)
VBA is completly new territory for me so excuse my novise questions.
 
Upvote 0
Go to the sheet that you want to apply this to, right-click on the Sheet tab name at the bottom of the screen, select "View Code", and paste this code in the VB Editor window that pops up:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
    
'   See if any updates to specified range
    Set rng = Intersect(Range("AE2:AE1500"), Target)
    
'   Exit sub if no changes to watched range
    If rng Is Nothing Then Exit Sub
    
'   Loop through changed cells
    Application.EnableEvents = False
    For Each cell In rng
'       See if cell is set to "Apple"
        If cell.Value = "Apple" Then
'           Add date stamp to column AG of same row
            cell.Offset(0, 2).Value = Date
        End If
    Next cell
    Application.EnableEvents = True
    
End Sub
This VBA code will automatically run whenever you make updates to the range AE2:AE1500.

The code should not affect the performance of your workbook, even for 1500 cells.
 
Upvote 0
Solution
Hi Joe,

This works like a charm.
I have one issue, that the date shows up in fontstyle "Roboto" and fontsize 8 in the cell in column AG.
What can I do to have the fontstyle "Calibri" and fontsize 11 on the dates in column AG?
 
Upvote 0
Hi Joe,

This works like a charm.
I have one issue, that the date shows up in fontstyle "Roboto" and fontsize 8 in the cell in column AG.
What can I do to have the fontstyle "Calibri" and fontsize 11 on the dates in column AG?
The code is not changing the font at all. If you are seeing it in that format, it is because your workbook has already pre-formatted that column that way (try typing anything into any blank cell in AG and you will see this). Simply highlight the whole column AG and change it to whatever format you want, and anything this code writes to the column will be in that format.
 
Upvote 1
Hi,
Seems like my excel is a bit buggy... It works fine now.
Thanks for the tip.
 
Upvote 0
Hi,

I added two columns before Column AE. Now the script doesn't work eventhough I change the code
from: Set rng = Intersect(Range("AE2:AE1500"), Target)
To: Set rng = Intersect(Range("AG2:AG1500"), Target)





Private Sub Worksheet_Change(ByVal Target As Range)

Dim rng As Range
Dim cell As Range

' See if any updates to specified range
Set rng = Intersect(Range("AE2:AE1500"), Target)

' Exit sub if no changes to watched range
If rng Is Nothing Then Exit Sub

' Loop through changed cells
Application.EnableEvents = False
For Each cell In rng
' See if cell is set to "Apple"
If cell.Value = "Apple" Then
' Add date stamp to column AG of same row
cell.Offset(0, 2).Value = Date
End If
Next cell
Application.EnableEvents = True

End Sub


What I am doing wrong?
 
Upvote 0
Have events been disabled?

Try manually running this procedure to re-enable them:
VBA Code:
Sub ReEnableEvents()
    Application.EnableEvents = True
End Sub
and then see if it works.
 
Upvote 1

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
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