[Solved] Insert today's date as a static date

Discuss the spreadsheet application
Locked
Nocton
Volunteer
Posts: 533
Joined: Fri Nov 05, 2010 10:27 am
Location: UK

[Solved] Insert today's date as a static date

Post by Nocton »

I want to automatically insert today's date in a cell, but I don't want it to change in the future. How can I best do that?

Regards,
Nocton
Last edited by RoryOF on Mon Mar 31, 2014 9:39 am, edited 2 times in total.
Reason: Added green tick [RoryOF, Moderator]
OpenOffice 4.1.12 on Windows 10
B Marcelly
Volunteer
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Insert today's date as a static date

Post by B Marcelly »

Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
Nocton
Volunteer
Posts: 533
Joined: Fri Nov 05, 2010 10:27 am
Location: UK

Re: Insert today's date as a static date

Post by Nocton »

Thank you for pointing me to Villeroy's solution. So I ended up with:

Code: Select all

    =IF(A1="";"";IF(B1="";TODAY();B1))
Very important to allow iterations [Tools>Options>Calc>Calcultation: Enable iterations.] as I had something similar but always got an error.

Regards,
Nocton
OpenOffice 4.1.12 on Windows 10
User avatar
Zizi64
Volunteer
Posts: 11360
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Solved] Insert today's date as a static date

Post by Zizi64 »

Macro solutions:

Code: Select all

Sub SetTodayDateToSelectedCellAsLocalizedFormula

rem The target cell will be formatted based on the local settings of the date

 Dim oCell As Object 
 Dim DateStr as string  
	DateStr = Date(DateValue(Now))
	oCell = ThisComponent.getCurrentSelection()
 	oCell.FormulaLocal = DateStr
End sub


Sub SetTodayDateToSelectedCellAsValue

rem In this case You need format the cell manually

 Dim oCell As Object   
	oCell = ThisComponent.getCurrentSelection()
 	oCell.setValue(DateValue(Now))
End sub


Sub SetTodayDateToSelectedCellAsFormattedString

rem The date "value" will be formatted as String (You can not use that string for calculations directly)

 Dim oCell As Object 
 Dim DateStr as string  
	DateStr = (Format(DateValue(Now),"YYYY-MM-DD"))
	oCell = ThisComponent.getCurrentSelection()
 	oCell.String = DateStr
End sub
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Alexys4530
Posts: 4
Joined: Fri Mar 12, 2021 11:31 pm

Re: [Solved] Insert today's date as a static date

Post by Alexys4530 »

Hi,
How would I make that macro aply to a specific cell?

Thank you
Zizi64 wrote:Macro solutions:


Sub SetTodayDateToSelectedCellAsValue

rem In this case You need format the cell manually

Dim oCell As Object
oCell = ThisComponent.getCurrentSelection()
oCell.setValue(DateValue(Now))
End sub
OpenOffice 4.1.7 on Windows 10
Locked