返回列表 上一主題 發帖

[發問] 請問如何將內容有中華民國日期的字眼抓出來轉成西元日期?

回復 20# c_c_lai
  1. Sub Ex_日期數值()
  2.     Dim i  As Long, xl_Year As Variant
  3.     With Range("B1:B" & [A1].End(xlDown).Row)
  4.         .Cells = "=MID(RC[-1],10,10)"
  5.         .Value = .Value
  6.         .Replace "發", ""
  7.         For i = 1 To .Count
  8.             xl_Year = Split(.Cells(i), "年")
  9.             xl_Year(0) = xl_Year(0) + 1911 & "年"
  10.             .Cells(i) = Trim(Join(xl_Year, ""))
  11.         Next
  12.         .Cells.Replace "年", "/", xlPart
  13.         .Cells.Replace "月", "/"
  14.         .Cells.Replace "日", ""
  15.         .Offset(, -1).Resize(, 2).Sort Key1:=.Cells(1), Order1:=xlAscending, Header:=xlNo
  16.         .Offset(, -1).Resize(, 2).Select         'Offset(, -1) ->左移1欄 'Resize(, 2)  ->擴充為兩欄
  17.         '.Cells.Sort Key1:=.Cells(1), Order1:=xlAscending, Header:=xlNo
  18.     End With
  19. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 21# GBKEE
感謝您的指導,我又學到了 .Offset(, -1).Resize(, 2)
的應用,實在非常實用。再次說聲謝謝!

TOP

本帖最後由 Hsieh 於 2013-6-6 23:47 編輯

回復 1# freeffly

  1. Function ChDate(DateStr As String)
  2. 'DateStr必須是包含"中華民國年月日"的字串
  3. Dim Mystr As String, s%
  4.    s = InStr(DateStr, "中華民國") + 4
  5.    Mystr = Mid(DateStr, s, InStr(s, DateStr, "日") - s + 1)
  6.    ChDate = CDate(Replace(Mystr, Val(Mystr) & "年", Val(Mystr) + 1911 & "年"))
  7. End Function
複製代碼
學海無涯_不恥下問

TOP

本帖最後由 c_c_lai 於 2013-6-7 07:12 編輯
回復  freeffly
Hsieh 發表於 2013-6-6 23:37

蠻實用的功能函數,感謝您。另外我加上了 Instr() 條件不成立時之處裡:
  1. Sub Test()
  2.     Dim txt As String
  3.    
  4.     txt = CEDate([A1])
  5.     MsgBox IIf(txt = "", """中華民國"" 字串並不存在!", "Year = " & txt)
  6. End Sub

  7. Function CEDate(DateStr As String)    ' 「C.E.」是「Common Era」的縮寫,意為「公元」
  8.     '  DateStr必須是包含"中華民國年月日"的字串
  9.     Dim Mystr As String, s%
  10.    
  11.     s = InStr(DateStr, "中華民國") + 4
  12.     If s = 4 Then CEDate = "": Exit Function
  13.     Mystr = Mid(DateStr, s, InStr(s, DateStr, "日") - s + 1)
  14.     CEDate = CDate(Replace(Mystr, Val(Mystr) & "年", Val(Mystr) + 1911 & "年"))
  15. End Function
複製代碼
否則會產生以下的錯誤訊息:
執行階段錯誤 '13':
型態不符合

TOP

回復 23# Hsieh
回復 21# GBKEE
我將 GBKEE 版大的模組、加上 Hsieh 版大提供的 Function,
兩者組合後還蠻實用的:
  1. Sub Ex_日期數值3()
  2.     Dim i  As Long, xl_Year As Variant
  3.     With Range("B1:B" & [A1].End(xlDown).Row)
  4.         For i = 1 To .Count
  5.             .Cells(i) = ChDate(.Cells(i).Offset(, -1))
  6.             If .Cells(i) <> "" Then .Cells(i).Offset(, 1) = CEDate(.Cells(i))
  7.         Next
  8.         '  .Offset(,-1): 左移 1 欄 (B -> A欄); Resize(,3): 從A欄起擴充為三欄 (A、B、C 三欄)
  9.         '  .Cells(1) = "2012年10月12日"  (B 欄), .Cells(1).Offset(, 1) = "2012/10/12"  (C 欄)
  10.         '  .Offset(, -1).Resize(, 3).Sort Key1:=.Cells(1), Order1:=xlAscending, Header:=xlNo
  11.         .Offset(, -1).Resize(, 3).Sort Key1:=.Cells(1).Offset(, 1), Order1:=xlAscending, Header:=xlNo
  12.     End With
  13. End Sub
複製代碼
  1. Function ChDate(DateStr As String)
  2.     '  DateStr必須是包含"中華民國年月日"的字串
  3.     Dim Mystr As String, s%
  4.    
  5.     s = InStr(DateStr, "中華民國") + 4
  6.     If s = 4 Then ChDate = "": Exit Function
  7.     Mystr = Mid(DateStr, s, InStr(s, DateStr, "日") - s + 1)
  8.     ChDate = Replace(Mystr, Val(Mystr) & "年", Val(Mystr) + 1911 & "年")
  9. End Function

  10. Function CEDate(DateStr As String)    '  「C.E.」是「Common Era」的縮寫,意為「公元」
  11.     CEDate = CDate(DateStr)
  12. End Function
複製代碼
謝謝兩位版大的鼎力幫忙。

TOP

回復 23# Hsieh


    謝謝超版回覆
   平常幾乎不會去用自訂函數方式
   又多一個可以研究的東西了
字典兩各字 還真難理解

TOP

        靜思自在 : 【為善競爭】人生要為善競爭,分秒必爭。
返回列表 上一主題