将星期几,月份,年年转换为d / m / y

时间:2019-04-19 12:41:25

标签: excel vba

我在excel中具有以下内容:

Public Sub Move_first_then_Process_Email()

'   // Declare your Variables
    Dim Inbox As Folder
    Dim SubFolder As Folder
'    Dim olNs As NameSpace
    Dim Item As Object
    Dim lngCount As Long
    Dim Items As Items

    Dim movedItem As MailItem

'   Not when developing
'    On Error GoTo MsgErr

'    Set Inbox Reference
'   Not needed when using Session
'    Set olNs = GetNamespace("MAPI")

     Set Inbox = Session.GetDefaultFolder(olFolderInbox).Folders("1 - Arquivos Temporarios")
    Set Items = Inbox.Items

'   // Set target folder
    Set SubFolder = Session.GetDefaultFolder(olFolderInbox)

'   // Loop through the Items in the folder backwards
    For lngCount = Items.Count To 1 Step -1

        Set Item = Items(lngCount)

        Debug.Print "Subject of Item: " & Item.Subject

        If Item.Class = olMail Then
'
'           // Mark As Read
            Item.UnRead = False

'           // Move Mail Item to target folder
'               and create a reference to the moved item
            Set movedItem = Item.Move(SubFolder)

            'Call the macro for moved email
            '************
            display_Subject movedItem
            '************

        End If

    Next lngCount

MsgErr_Exit:
    Set Inbox = Nothing
    Set SubFolder = Nothing
    Set Item = Nothing

    Exit Sub

'// Error information
MsgErr:
    MsgBox "An unexpected Error has occurred." _
         & vbCrLf & "Error Number: " & Err.Number _
         & vbCrLf & "Error Description: " & Err.Description _
         , vbCritical, "Error!"
    Resume MsgErr_Exit
End Sub

Private Sub display_Subject(ByRef mvItem As Object)
    If mvItem.Class = olMail Then
        Debug.Print "Subject of movedItem: " & mvItem.Subject
        Debug.Print
    Else
        Debug.Print "Not a mailitem."
    End If
End Sub

我想将其转换为标准的dd / mm / yy格式。

(在excel或VBA中)最简单的方法是什么?

2 个答案:

答案 0 :(得分:2)

要使用Excel的CDate function,您首先必须摆脱标记日期的前导文本。假设这始终带有逗号和空格,那么您可以执行以下操作:

Sub changedtformat()
Dim rawdate As String, cleandate As Date, resultdate As Date

rawdate = "Saturday, June 11 2011"
cleandate = Mid(rawdate, InStr(1, rawdate, ",") + 2, Len(rawdate))
resultdate = CDate(cleandate)
MsgBox resultdate 

End Sub

答案 1 :(得分:1)

A1 中输入文字,在 B1 中输入:

=DATEVALUE(SUBSTITUTE(MID(A1,FIND(" ",A1)+1,9999)," ",", ",2))

并按照自己喜欢的格式进行格式化。我们只是创建一个DATEVALUE()可以处理的字符串。