多宏通信

时间:2014-11-06 16:42:19

标签: vba date excel-vba formatting excel

我目前正试图在VBA中自我教授宏观编码的伟大世界,但在尝试处理3个宏时遇到了绊脚石,我希望将其作为1来处理,但代码似乎也太过分了在这个阶段我很复杂。

我需要的是将美国日期格式mm / dd / yyyy的数据转换为英国日期格式dd.mm.yyyy并将/更改为。同时理想地覆盖原始数据。

这是我目前在单独的模块中所拥有的:

Sub FixFormat()

'display a message with an option if US date formats are
'included in the data
 MsgBox "US Date Formats Included", vbQuestion + vbYesNo, "Addresses"
If Response = Yes Then MsgBox "Delimit Process Needed", vbOKOnly, "Addresses"
If Response = No Then MsgBox "End", vbOKOnly
End

End Sub   

    Sub FixDates()

Dim cell As Range
Dim lastRow As Long

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

For Each cell In Range("A1:A" & 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 = RegexReplace(cell.Value, _
        "(\d{2})/(\d{2})/(\d{4})", "$3.$1.$2")
    End If
    cell.NumberFormat = "yyyy-mm-d;@"

Next

    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

有没有办法在不必运行2个单独的宏的情况下执行此操作?

1 个答案:

答案 0 :(得分:2)

是的,您可以Call作为消息框的结果运行您想要运行的子例程。

Sub FixFormat()

'display a message with an option if US date formats are
'included in the data
If MsgBox("US Date Formats Included", vbQuestion + vbYesNo, "Addresses") = 6 Then
    MsgBox "Delimit Process Needed", vbOKOnly, "Addresses"
    Call FixDates
Else
    MsgBox "End", vbOKOnly
End If

End Sub

有关MsgBox功能的更多信息,请参阅此链接:http://msdn.microsoft.com/en-us/library/139z2azd(v=vs.90).aspx