0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

令和時代のVBA元号 InitialEraYearは1年で統一したうえでExcelとWordの和暦日付入力設定する

Posted at

結局元年でも元年にならない

とりあえず自分のところではExcelはならず、Wordはなりました。

なのでこのMicrosoftの記述はあまり信用できないです

歴史的な実践慣習において、元号の 1 年目については、“起点” または“開始”を意味する漢字特殊文字の“元” が数字の “1 の代わりに使用されます。 1年目の“元年”は、グレゴリオ暦年の最終日にあたる 12 月 31 日まで続きます。

Windows は、元号の 1 年目として “元年” と “1 年” の両方をサポートします。サポート提供中の Windows 10 Version 1809 およびそれ以前のすべての Windows については、元年は既定で OFF になりますが、有効にできます。

元年を有効にするには、[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\Calendars\Japanese] で InitialEraYear レジストリ キーを “元年” に設定します。

以上からInitialEraYearは1年でOK

元年を無効にするには、InitialEraYear レジストリ キーを “1年” に設定します。
Excel 2010 の VBA での元号表記が1年にならない
https://answers.microsoft.com/ja-jp/msoffice/forum/all/excel-2010-%E3%81%AE-vba/c3f4592b-a861-43d7-a064-e7660ac90393
Accessなどは受け付けないとしています。更新でバグが起きれば令和元年に対応しないことは十分におきます。
このため1年で設定した方がよいです。
またPCの設定、特に日付が安定しないのは危険で数字で扱える設定で、切り替えた方が安全です。
本当はオプションでできた方がいいと思います。

https://www.exict.yokohama/learn/knowledge/ict/reiwa
何とかはいらない、というちきりんやほりえもん的なびゅうプラザはいらないとかいう人が元号表記をいらないという人は見たことがない。本当は政権がいらないといわなきゃいけない。けどこういう人たちは言わないし、国民も行動しない。それが今回のように今後日付でシステムが混乱するコストがかかるようになったわけです。あなたの苦労はあなたがまともな政策を行わない政権を選んだ怠慢による罰です。あなたがどんなに効率的に賢く生きようと思っても根本を改善する気がないのでは無駄なコスト生じるという例です。

としておいて、Word、Excelはどうするか

今回はレジストリの前提は1年ですが、別に元年でも構いません。元年で表記する、というのがテーマなので。これを表示形式をマクロで設定して対応します。

Excelの表示形式

次の表示形式使用する

https://hamachan.info/win10-excel-reiwa/

[<43586]ggge"年"m"月"d"日";[<43831]"令和元年"m"月"d"日";ggge"年"m"月"d"日"

3つカスタマイズするのが令和スタイル

曜日や全角はまたカスタマイズが必要です。またカスタマイズは3つとも書きます。試験年月日 令和元年5月1日という表示形式を設定する
ダブルクォーテーションの位置が違うことに注意してください。

[<43586]"試験年月日 "ggge"年"m"月"d"日";[<43831]"試験年月日 令和元年"m"月"d"日";"試験年月日 "ggge"年"m"月"d"日"

選択したセルに、上記ユーザー定義標識を設定するマクロ(Excel)

[<43586]ggge"年"m"月"d"日";[<43831]"令和元年"m"月"d"日";ggge"年"m"月"d"日"

これは一つ一つ入力するのは大変なので、選択範囲に入るようにします。

Sub InsertNumberFormatToActiveRange()
'For Excel
' When Japanese Era Reiwa has been already written "
' Activate whitch You wanna set Numberformat(Local), and run this VBA Macro
Dim Rng As Range, R As Range
Set Rng = Selection
For Each R In Rng
Debug.Print R.Value, R.Formula, R.NumberFormatLocal, R.NumberFormat
Selection.NumberFormatLocal = ""
Selection.NumberFormatLocal = _
"[<43586]ggge""""m""""d"""";[<43831]""令和元年""m""""d"""";ggge""""m""""d"""""
Next
End Sub

Activeなシートの値が日付と判定できるセルの表示形式を確認する(Excel)

Sub ShowNumberFormat()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = ActiveSheet
Dim R As Range, URng As Range: Set URng = ws.UsedRange
For Each R In URng
If IsDate(R.Value) Then
Debug.Print ws.Name, "++++", R.Address
Debug.Print R.NumberFormat
Debug.Print R.NumberFormatLocal
End If
Next
End Sub

Wordは2つの方法

きょうの日付を現在の位置でフィールドコードで挿入し更新をしないようにする

http://office-qa.com/Word/wd1000.htm

{ IF {Date @ "e" } = 1 {Date @ "ggg元年M月d日" } {Date @ "ggge年M月d日" }}

ただこの式は、Ctrl+F9を4回使い、間違うとやり直す方がいいので、マクロで入力します。
挿入したい位置で実行します。



Sub InsertReiwaDateField()
 Word

ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.TypeText Text:="IF "
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=" "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.TypeText Text:="Date \"
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=" "
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="@ """""
Selection.MoveLeft Unit:=wdCharacter, Count:=2
Selection.TypeText Text:=" "
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="e"
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=" "
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.TypeText Text:=" = 1 "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.MoveLeft Unit:=wdCharacter, Count:=3
Selection.TypeText Text:=" "
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.TypeText Text:=" Date \@"
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=" """""
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:="令和元年M月d日"
Selection.MoveRight Unit:=wdCharacter, Count:=5
Selection.TypeText Text:=" "
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.TypeText Text:="Date \@ ""ggge年M月d日"" "
Selection.Fields.Update
Selection.Fields.ToggleShowCodes
Selection.Fields.ToggleShowCodes
Selection.Fields.Locked = True ’ここで更新が不可能になる
End Sub

日付コンテンツコントロール(拡張子docx,docm系のみ)

これは入力日付をInputBoxで受け取り、日付を判断して表示形式を決定して挿入

おかげで今日の日付に縛られることはありません。ただし、拡張子はWord2007以降の形式です。doc形式は動きません。
また、日付コンテンツコントロールは任意の日付が使えますが、そもそも表示形式で分けられないため、事前にマクロ側で判定して表示形式を変えています。

欠点

この方式は日付を変えるとうまくいかないので2019年5月1日から2019年12月31日の間の日付に訂正する場合は日付コンテンツコントロール自体を削除して入力しなおしになります。

Sub InsertReiwaDateCC()
'For Word
'Registry Reiwa

Dim wDoc As Word.Document: Set wDoc = ActiveDocument
Dim wCC As Word.ContentControl
Dim DT As Date
DT = CDate(InputBox("input date", "InputDate", "2019/12/1"))
If CLng(DT) > 43830 Or CLng(DT) < 43586 Then
Set wCC = Selection.Range.ContentControls.Add(wdContentControlDate)
wCC.Range.Text = Format(DT, "yyyy/mm/dd")
wCC.DateCalendarType = wdCalendarJapan
wCC.MultiLine = True
Selection.ParentContentControl.DateDisplayFormat = "ggge年M月d日"
Else
Set wCC = Selection.Range.ContentControls.Add(wdContentControlDate)
wCC.Range.Text = Format(DT, "yyyy/mm/dd")
wCC.DateCalendarType = wdCalendarJapan
wCC.MultiLine = True
Selection.ParentContentControl.DateDisplayFormat = "ggg元年M月d日"
End If
wCC.LockContents = True 
End Sub
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?