工作表之间的DateValue格式问题

时间:2016-11-01 01:24:40

标签: excel vba excel-vba date format

我遇到了从一个工作表到另一个工作表的日期问题。当使用下面的公式时,如果日期符合美国和澳大利亚格式,它决定将其转换为美国(mm / dd / yyyy,当我需要它为dd / mm / yyyy时)。

源单元格和目标单元格都是相同的格式,单元格格式本身不会通过vba进行更改,它只会混淆一天和一个月。

Sub BOCMS()

Dim date1 As String
Dim date2 As String
date1 = Format(DateValue(Sheet1.Range("L13").Value), "dd/mm/yyyy")
date2 = Format(DateValue(Sheet1.Range("L14").Value), "dd/mm/yyyy")

Workbooks.Open (ThisWorkbook.Path & "\Manual date res acts\BOC - Res Activity Report.xlsm")
Workbooks("BOC - Res Activity Report.xlsm").Activate

Workbooks("BOC - Res Activity Report.xlsm").Worksheets("Res Activity Report").Range("O1").Select
Selection.ClearContents
Selection.Value = Format(date1, "dd/mm/yyyy")
Workbooks("BOC - Res Activity Report.xlsm").Worksheets("Res Activity Report").Range("Q1").Select
Selection.ClearContents
Selection.Value = Format(date2, "dd/mm/yyyy")


ActiveWorkbook.RefreshAll
Application.Run "'BOC - Res Activity Report.xlsm'!RunReport"
End Sub

任何提示都会很棒

1 个答案:

答案 0 :(得分:0)

我没有两个安装程序可以测试不同的本地,所以这是未经测试的,但应该关闭

Sub BOCMS()
    Dim date1 As Double
    Dim date2 As Double
    Dim wb1 As Workbook, wb2 As Workbook

    date1 = Sheet1.Range("L13").Value
    date2 = Sheet1.Range("L14").Value

    Set wb1 = Workbooks.Open(ThisWorkbook.Path & "\Manual date res acts\BOC - Res Activity Report.xlsm")
    Set wb2 = Workbooks("BOC - Res Activity Report.xlsm")

    With wb2.Worksheets("Res Activity Report").Range("O1")
        .ClearContents
        .Value = date1
        .NumberFormat = "dd/mm/yyyy"
    End With

    With wb2.Worksheets("Res Activity Report").Range("Q1")
        .ClearContents
        .Value = date2
        .NumberFormat = "dd/mm/yyyy"
    End With


    ActiveWorkbook.RefreshAll
    Application.Run "'BOC - Res Activity Report.xlsm'!RunReport"
End Sub