« 2018年4月 | トップページ | 2018年6月 »

2018年5月

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.

| | コメント (0) | トラックバック (0)

2018年5月20日 (日)

今日という日付の入力方法いろいろ

Excelを使っていて
入力した日付を記録する機会は多いと思います。
いちいち手入力するのは手間ですね。

以下に、
今日の日付を入力する方法を挙げていきます。

まずは、言うまでもないですが手で入力する方法
5月20日と入力したいとすると
セルに「5/20」と入力すれば
入力した年の日付のデータとして入力されます。
Vba20180520a


次は、TODAY関数を使った方法
「=TODAY()」と入力すれば表示される値は今日の日付になります。
Vba20180520b

ただし、例えば3日後に表示される日付は
3日後の日付に更新されてしまうので
日付のコピペ元としては使えます。
あと、関数に必要な引数がないので
「()」を忘れやすいので注意してください。
Vba20180520c

次は、リストとTODAY関数の合わせ技
セルA1に「=TODAY()」と入力しておき
常に今日の日付が表示されるようにします。

このセルをセルA2において
ドロップダウンリストで選べるようにします。
そうするとリストで選択した時の日付が表示されます。
この日付は、リストで再度選ばないと更新されません。

リストの作成するには
[データ」-「入力規則」をクリックしてください。
次の画像のウィンドウが表示されます。
Vba20180520d

入力値の種類を「リスト」
元の値のところにTODAY関数を入力したセルを指定してください。
今回の例ではA1セルを指定します。
Vba20180520e

セルA2にリストが作成されました。
下向き三角を押してリストを表示させると
今日の日付が表示されるので
これを選択すると今日の日付が入力されます。

日付として表示されない場合は
[セルの書式設定]で[表示形式]を日付にしてください。
また、リストを設定するとセルの値はDeleteはできますが、
選択肢以外の値は入力できなくなります。

最後はVBAを使った方法
アクティブなシートでセルに値を入力すると
A列に自動で日付が入力されるようにします。

具体的には、ワークシートのイベントを使用します。

「イベント」とは
  シートの値が変わった。
  シートがアクティブになった。
などの時を言います。

特定のシートに関連付けられていないマクロは
「標準モジュール」に記述します。
一方、ワークシートのイベントは
特定のワークシートにのみに関連するもので、
「標準モジュール」には記述しません。

「イベント」を使用してマクロを実行するには
「イベントプロシージャ」を使用します。
イベントプロシージャの作成するには
まず、VBEのウィンドウで
イベントを使用したいワークシートをダブルクリックして
コード ウィンドウを開きます。
Vba20180520f

下の画像の様に
コード ウィンドウの左側のドロップダウンリストから
「Worksheet」を選択します。
Vba20180520g

次に、コード ウィンドウの右側のドロップダウンリストから
「Change」を選択します。
Vba20180520h

これで、ワークシートの値が変更されたときに実行される
「Worksheet_Change」イベントプロシージャが作成されました。
ここにシートの値が変更されたときに
日付を入力するコードを書きます。

自動で作成される「Worksheet_SlectionChange」イベントプロシージャは
今回は使用しないので消してください。
Vba20180520i

コードはこちら

自動で日付を入力するコード:

Private Sub Worksheet_Change(ByVal Target As Range)
'A列以外のシートの値が変更されたら
'A列に日付入力

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

実行結果:セルB1に入力したら、セルA1に日付が入力された。
Vba20180520j

Target.Rowで値が変更されたセルの行番号、
Target.Columnで値が変更されたセルの列番号が取得できます。

| | コメント (0) | トラックバック (0)

« 2018年4月 | トップページ | 2018年6月 »