Excel VBA中消息框中的日期格式

时间:2016-06-16 17:47:42

标签: excel-vba date-format msgbox vba excel

我找到了一种基于条件显示消息框的方法,但是,出现的日期格式不是我想要显示的内容或工作表中显示的内容。 // Fictional code. Assigns into nonOptional if non-nil; does nothing if nil: nonOptional =? mightBeNil // Equivalent but less concise real code: if let notNil = mightBeNil { nonOptional = notNil } 是我想要显示为“mmmm dd,yyyy”的日期值,但它显示为“m / d / yyyy”。我的实际Excel数据的日期为“mmmm dd,yyyy”。

我尝试了多种方式来格式化日期,但我不断收到错误消息。是否可以在消息框中更改日期格式?

Cells(i, 4).Value

2 个答案:

答案 0 :(得分:0)

以下内容应该解决它

MsgBox Workbooks("Issuers.xlsm").Sheets("Summary of Covered Companies").Cells(i, 3).Value & " is issuing their next financial statement tomorrow (" & _
        Format(Workbooks("Issuers.xlsm").Sheets("Summary of Covered Companies").Cells(i, 4).Value,"[$-409]mmmm d, yyyy;@") & ")."

PS:使用宏录制器获取大多数excel命令,如数字格式

答案 1 :(得分:0)

我认为你可以通过使用 .text属性 intead of .value

获得你想要的东西

试试这个例子:

Sub Alert() 

    Dim i       As  Long 
    Dim wb      As Workbook
    Dim ws      As Worksheet

    Dim strDisplayDate  As String
    Dim strCompany      As String

    i = 3

    Set wb = Workbooks("Issuers.xlsm")
    Set ws = wb.Sheets("Summary of Covered Companies")

    With ws
        While .Cells(i, 5) <> ""

            If .Cells(i, 5).Value = 1 Then

                strCompany = .Cells(i, 3).Value

                ' Use text not value to store what's displayed on screen            
                strDisplayDate = .Cells(i, 4).Text 

                strMessage = strCompany & " is issuing their next financial statement tomorrow (" & strDisplayDate & ")."
                MsgBox strMessage

            End If

            i = i + 1

        Wend
    End With

End Sub

如果您确实想自己格式化值,可以使用.Value2获取基础值,如Format(.Cells(i,4,Value2),"mmmm dd, yyyy")