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

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
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
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
Sorry for a newbie question but how do I that?
Should I paste it in before the previous code or after previous code?
It makes no difference where you put this code. It is its own separate procedure.
You can put it anywhere. The important thing is to run it.

A little background...
If you note, the code that I created for you has a line like this at the beginning of the code:
VBA Code:
Application.EnableEvents = False

What this does is shut off events (like "Worksheet_Change") while the code runs. Why do we do this? To avoid getting caught in possible endless loops.
A "Worksheet_Change" event procedure is VBA code that runs automatically when a cell is manually updated. But if the code itself is making updates to cells (like ours is), those changes will cause the "Worksheet_Change" event procedure to fire again (the code is triggering another iteration of itself to run!). Depending on what the code does and how it is written, you could get caught in an endless loop, and your Excel will lock up! We obviously don't want this to happen!

So, the typical way to handle this is to temporarily turn events off with the line above BEFORE your code updates any cells, and then turn it back on AFTER the cell updates with a line like:
VBA Code:
Application.EnableEvents = True

However, if you encounter any errors or breaks in your code where the code shuts off events, but never gets to the line that turns them back on, they will be shut off and your "Worksheet_Change" event procedure will not work automatically anymore until you turn it back on. That is what that little procedure I can you in my last post does.

Alternatively, you could close out of Excel altogether, and then re-open it to reset everything.
 
Upvote 1
Your welcome.

So, did that fix the issue, or is it still not working?
 
Upvote 1
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,
Seems like my excel is a bit buggy... It works fine now.
Thanks for the tip.
 
Upvote 0

Forum statistics

Threads
1,215,956
Messages
6,127,931
Members
449,411
Latest member
AppellatePerson

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