展开当前范围以包含多个非触摸列

时间:2016-10-19 17:48:29

标签: arrays regex excel vba loops

如何动态扩展当前范围以包含多个非触摸列?

我认为需要包含动态多维数组来完成此任务。

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 

1 个答案:

答案 0 :(得分:0)

是的,这是一个很好的复杂方法。或者你可以

范围(范围(&#34; A1&#34;),ActiveCell.SpecialCells(xlLastCell))。地址