2016-10-19 18 views
0

主要問題-A更改日期格式YYYY-MM-DD(多個列)動態數組

伸出我因爲我對如何動態地擴展電流範圍的範圍內的問題包括多個(非觸摸列)。我在想這需要包含一個動態的多維數組來完成這個任務。

宏代碼:

Sub Convert Dates() 

Dim Answer As String 
Dim Cell As Range 
Dim Last Row As Long 

Answer = MsgBox("Are you sure you want to run the macro",vbYesno,"Run the Program".) 

If Answer = vbYes Then 

    lastRow = Range ("Q" & Rows.Count).End(xlUp).Row 

    For Each cell In Range ("Q2:@" & lastRow) 

     If Instr(cell.value, ".") <> 0 Then 
     Cell.value = RegexReplace(cell.Value, _ 
     "(\d{2})\.(\d{2})\.(\d{4})", "$3-$2-$1") 
     End if 

     If Instr (cell.value, "/") <> 0 Then 
     cell.Value = RegexRepalce(Cell.Value,_ 
     "(\d{2})/(\d{2})/(\d{4})". "$3-$1=$2") 
     End if 

     cell.NumberFormat = "yyyy-mm-d;@" 

    Next 
    End if 
End Sub 

    Function RegexReplace(ByVal text As String, _ 
       ByVal replace_what As String, _ 
       ByVal replace_with As String) As String 
    Dim RE As Object 
    Set RE = CreateObject("vbscript.regexp") 
    RE.Pattern = replace_what 
    RE.Global = True 
    RegexReplace = RE.Replace(text, replace_with) 
    End Function 
+0

功能REGEXREPLACE(BYVAL文本,作爲字符串_ BYVAL replace_what,作爲字符串_ BYVAL 作爲replace_with字符串)作爲字符串 昏暗RE作爲對象 集RE =的CreateObject( 「vbscript.regexp」) RE.Pattern = replace_what RE.Global =真 REGEXREPLACE = RE.Replace(文本,replace_with) 端功能 – Bmathfun

+0

評論下面添加了功能,因爲網站墜毀在我身上。 – Bmathfun

+2

請不要在評論中放入代碼。編輯您的原始帖子以包含它。 –

回答

0

呀那是一個很好的複雜的方式來做到這一點。或者你可以只

(範圍( 「A1」),ActiveCell.SpecialCells(xlLastCell))。解決