ユーザー定義関数として作成してみました。
標準モジュールに置いて下さい
適当なセルで
=convertDate(Sheet1!A1)
のように使います。
セルの書式設定の配置で折り返して全体を表示するにチェックを付けて置いて下さい。(分までが入るセル幅にしておきます)
----------------------------------------------------------------
Public Function convertDate(r As Range) As String
Dim dateTxt As String
Dim regEx, matches, match
Dim startDate As String, endDate As String
dateTxt = StrConv(r.Value, vbNarrow) '数値を半角に
Set regEx = CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.Pattern = "(\d+).+?(\d+).+?(\d+).+?(午前|午後)(\d+).+?(\d+)"
Set matches = regEx.Execute(dateTxt)
Set match = matches(0)
With match
startDate = .SubMatches(0) & ". " & .SubMatches(1) & ". " & .SubMatches(2) & ". " & _
.SubMatches(4) + IIf(.SubMatches(3) = "午後", 12, 0) & ":" & .SubMatches(5)
End With
If matches.Count > 1 Then
Set match = matches(1)
With match
endDate = .SubMatches(0) & ". " & .SubMatches(1) & ". " & .SubMatches(2) & ". " & _
.SubMatches(4) + IIf(.SubMatches(3) = "午後", 12, 0) & ":" & .SubMatches(5)
End With
Else
endDate = ""
End If
If endDate = "" Then
dateTxt = startDate
Else
dateTxt = startDate & vbLf & "~" & vbLf & endDate
End If
convertDate = dateTxt
End Function
補足
返事が遅れてしまいすいません。本当に助かりました。!!すごくうれしいです。 一つ質問してもよろしいでしょうか? ちゃんと型変換もでき、私のやりたかったことができました。! それでエラー処理として、まったく違う文字をうってしまったり スペースが多すぎたり、型があわなかったりすると、エラーになってしまいます。 このときにエラーメッセージとして 『スペースが多いか、型が違います』みたいなエラーメッセージを出してやり直しを促すような処理をほどこしたいのですが、 どのプログラムの部分をどう追加すればよいのかわからず 悪戦苦闘しております。どうかお力をかしていただけないでしょうか。 お願いします。困っていまして・・