LoginSignup
0
1

More than 3 years have passed since last update.

【VBA】8桁の数字から、日付の変換と曜日を求める方法

Posted at

今回は、8桁の数字から、日付の変換と曜日を求める方法です。

サンプルデータは以下の図をご覧ください。
サンプル

↓実演動画
https://youtu.be/uatMG42tjQg

ソースコードは下記の通りです。


Sub 日付の変換と曜日計算()

  ' 日付の変換
    Cells(2, 2) = DateValue(Format(Cells(2, 1), "0000/00/00"))

    ' 日付から曜日を求める
    Cells(2, 3) = Format(Cells(2, 2), "aaaa")

End Sub

詳細を説明します。

Cells(2, 2) = DateValue(Format(Cells(2, 1), "0000/00/00"))

DateValue関数は、文字列を日付に変換する関数です。
Format関数は、値を指定した形式の文字列に変換してる関数です。
第1引数には対象の値、第2引数には、指定したいフォーマットになります。

DateValue関数の詳細はこちら
https://www.tipsfound.com/excel/04datevalue

Format関数の詳細はこちら
https://www.tipsfound.com/vba/05format

Cells(2, 3) = Format(Cells(2, 2), "aaaa")

日付の変換が完了したら、その値から曜日を求めます。
曜日を求めるにも、Format関数を使用します。

今回は、「〇曜日」と表示したいため、第2引数には"aaaa"を指定することで、
正しく表示されました。

フォーマットの種類については、以下のサイトが参考になると思います。
https://www.sejuku.net/blog/33422

以上が、8桁の数字から、日付の変換と曜日を求める方法でした。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1