« 今日という日付の入力方法いろいろ | トップページ | セル内の文字列を改行する »

2018年5月26日 (土)

Various input methods for today's date

Using Excel,
I think that there are many opportunities
to record the date you entered.
It is troublesome to manually input each time.

Here are a several ways to enter today's date.

First of all, needless to say,
but I will show how to enter Today's date by hand.
If you wanted to input 20th May,
you might enter "5/20" in the cell.
It is changed as this year's date.

Vba20180520a

The second way is to use the TODAY function.
When you enter "= TODAY ()" into the cell, today's date is displayed.
Vba20180520b

However, for example,
the date displayed after 3 days
is updated to the date after 3 days.
It can be used as a copy source of the date.
Since there are no argument for the function,
Please be careful not to forget "()".
Vba20180520c

The third way is to use the list and TODAY function together.
Enter "= TODAY ()" in cell A1,
Then the date of today will be displayed.

For being able to select the value of cell A1,
Create a drop-down list in cell A2.
Then you can choose today's date in the list.
This date will not be updated unless you select it again in the list.

How to create a list:
Click on [Data] - [Input rule] in that order.
The window shown in the next image is displayed.

Vba20180520d
Specify the type of input value as "list" and the original value as the cell in which TODAY function was input. In this example, specify cell A1 as the original value.

Vba20180520e
A list has been created in cell A2.
Press the downward pointing triangle to display the list.
Then choose today's date.

If it is not displayed as a date,
In [Cell formatting], select [Date format] as the date.
Also, if you set the list, you can delete the value of the cell,
can not enter values other than options.

The last way is to use VBA
When you enter a value in a cell
The date is automatically entered in column A by this method.

Specifically, we will use the Worksheet Event.

"Event" is about when
The value of the sheet was changed.
Or The seat became active.

Macros not associated with a particular sheet
is described in "Standard module".
On the other hand,
Worksheet Events is relevant only to specific worksheets.
It is not described in "Standard Module".

To run a macro using "Event"
Use "Event procedure".
To create an Event Procedure
First, in the VBE window
Double click on the worksheet whose event you want to use,
then the code window is opened. Vba20180520f

Like the image below,
In the drop-down list on the left side of the code window
Select "Worksheet".

Vba20180520g
Next, in the drop-down list on the right side of the code window
Select "Change".

Vba20180520h
The "Worksheet_Change" event procedure has been created.
This is done when the worksheet values are changed.
In this event procedure,
Write a code to enter the date when the sheet's value is changed.

The automatically created Worksheet_SlectionChange event procedure,
Please delete it for not using it this time.

Vba20180520i

Code here

Code to automatically enter Today's date:

Private Sub Worksheet_Change(ByVal Target As Range)
'When the value of a sheet other than row A is changed
'Date is entered in column A

    If Target.Column <> 1 Then
        Cells(Target.Row, 1) = DateValue(Now)
    End If
   
End Sub

Result: By entering "123" in cell B1,
Today's date was entered in cell A1.
Vba20180520j

You can get the row number of the cell whose value was changed with "Target.Row",
the column number with "Target.Column"S.

|

« 今日という日付の入力方法いろいろ | トップページ | セル内の文字列を改行する »

コメント

この記事へのコメントは終了しました。

トラックバック


この記事へのトラックバック一覧です: Various input methods for today's date:

« 今日という日付の入力方法いろいろ | トップページ | セル内の文字列を改行する »