LoginSignup
3
0

More than 3 years have passed since last update.

ACCESSで日付を空白埋めする

Last updated at Posted at 2019-08-15

ことのはじめ

とあるシステムでACCESSでとあるボタンを押したら、あらかじめテーブルに登録してあるSQLが実行されて、その内容でCSVを出力して
さらにそれをWORDに差し込んでさらにPDF化みたいなことをしている

そこで月ごとの金額明細みたいな表を作った時に、日付が
2016/11/10
2016/12/9
2017/1/8
2017/2/10
みたいな感じで縦に並べた時、幅がバラバラになる
なので書式をyyyy/mm/ddにして0詰めしたいところだが、お客様的にはそれが見栄えが良くなくてNGらしい(気持ちは分かる)
word側で等幅とかにしてみたけど余計に気持ちがわるい

どうにかyyyy/m/dとyyyy/mm/ddの幅を同じにしたい

で、どうしたものかと検索かけたら同じことをやりたい人がヒット(゚∀゚ )
http://www.accessclub.jp/bbs/0085/beginers31302.html

…うげえ、めんどくせえ

いや、この方法は思いついたけどさあ。。。
やりたくないからなんかスマートなformat方法はないかと調べたわけなんすよ。。。

ということで諦めて作ることにした

やりたいこと

イメージは

2017/1/8→2017/ 1/ 8
2017/2/10→2017/ 2/10
みたいな感じ
あと、西暦だけじゃなくて和暦でも同じような感じにしたい
そして年月日だけじゃなく年月とかでもちゃんと表示できるようにしたい

要はいろんな日付書式で年や月や日が1ケタの場合空白で出来るようにしたい
そしていちいち&とかで繋げるんではなく、設定が楽にできるようにしたい

完成品

ということで、普通のformat関数の書式設定を使いつつどうにかできないかなとVBAゴネゴネしてたらいつの間にかできてた

'------------------------+'
'+ 共通モジュール         
'+
'+   Name : dateFormat
'+
'+   月や日が1桁の場合空白で充填する
'+ 引数
'+   a:日付項目(中身がNULLでも可)
'+   b:日付書式
'+
'+ 使い方
'+ dateFormat (now(),"yyyy/m/d日")→2018/ 9/ 5
'+ dateFormat (now(),"ggge(yyyy)年m月d日")→ 平成30(2018)年 9月11日
'+--------------------------------------------------------+'


Public Function dateFormat(a As Variant, b As String) As String
    If Nz(a, "") <> "" And IsDate(a) = True Then
        b = StrConv(b, vbLowerCase)
        b = Replace(b, "ggg", Format(a, "ggg"))
        b = Replace(b, "gg", Format(a, "gg"))
        b = Replace(b, "g", Format(a, "g"))
        b = Replace(b, "yyyy", Format(a, "yyyy"))
        b = Replace(b, "ee", Format(a, "ee"))
        b = Replace(b, "e", Format(Format(a, "e"), "@@"))
        b = Replace(b, "mm", Format(a, "mm"))
        b = Replace(b, "m", Format(Format(a, "m"), "@@"))
        b = Replace(b, "dd", Format(a, "dd"))
        b = Replace(b, "d", Format(Format(a, "d"), "@@"))
        dateFormat = b
    ElseIf Nz(a, "") = "" Then
        b = StrConv(b, vbLowerCase)
        b = Replace(b, "ggg", "  ")
        b = Replace(b, "gg", " ")
        b = Replace(b, "g", " ")
        b = Replace(b, "yyyy", "    ")
        b = Replace(b, "ee", "  ")
        b = Replace(b, "e", "  ")
        b = Replace(b, "mm", "  ")
        b = Replace(b, "m", "  ")
        b = Replace(b, "dd", "  ")
        b = Replace(b, "d", "  ")
        dateFormat = b
    Else
     Exit Function
    End If
End Function

使い方

使い方例にあるように
dateFormat (now(),"yyyy/m/d")→2018/ 9/ 5
dateFormat (now(),"ggge(yyyy)年m月d日")→ 平成30(2018)年 9月 1日
みたいな感じで変換できる!!!

そして日付が無いときにも対応!!!えらい!!!
dateFormat (null,"yyyy/m/d")→ / /
dateFormat (null,"ggge(yyyy)年m月d日")→   ( )年 月 日

注意

自分の使う書式しか入ってないので、他の日付書式とか曜日とか入れたい人は

b = Replace(b, "aaa", Format(a, "aaa"))

みたいな感じで入れるとよい

補足

実際にwordとかに差し込む場合は一つ注意点があって、段落オプションで
空白の自動調整的なのをオフにする必要がある
あとこれ、試してないけどexcelでも使えるんじゃないかな

3
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
3
0