Excel VBA 消息框中的日期格式

Date format in a message box in Excel VBA

我找到了一种根据条件显示消息框的方法,但是,出现的日期格式不是我想要显示的,也不是我的工作表中显示的。 Cells(i, 4).Value 是我想显示为 "mmmm dd, yyyy" 的日期值,但它显示为 "m/d/yyyy"。我的实际 Excel 数据的日期为 "mmmm dd, yyyy"。

我尝试了多种格式化日期的方法,但总是收到错误消息。是否可以更改消息框内的日期格式?

Sub Alert()
    Dim i As Long
    i = 3

    While Workbooks("Issuers.xlsm").Sheets("Summary of Covered Companies").Cells(i, 5) <> ""

    With Workbooks("Issuers.xlsm")

        If .Sheets("Summary of Covered Companies").Cells(i, 5).Value = 1 Then

             MsgBox Workbooks("Issuers.xlsm").Sheets("Summary of Covered Companies").Cells(i, 3).Value & " is issuing their next financial statement tomorrow (" & _
            Workbooks("Issuers.xlsm").Sheets("Summary of Covered Companies").Cells(i, 4).Value & ")."

        End If

    End With

   i = i + 1

   Wend

End Sub

下面应该修复它

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 命令,例如数字格式

我想你可以通过使用 .text 属性 而不是 .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")