【问题标题】:Change date in MM-DD-YY format in a formula within code to the next day将代码中公式中的 MM-DD-YY 格式的日期更改为第二天
【发布时间】:2015-02-28 21:35:02
【问题描述】:

我正在使用此代码(作为更大宏的一部分)来引用特定日期文件中的单元格。参考文件是使用 mm-dd-yy 格式保存到共享驱动器的每日备份版本。

我想使用 VBA 代码(使用 mm-dd-yy)更改此公式以引用第二天的文件,即将“05-07-14”更改为“05-08-14”:

Range("CL2").Select
ActiveCell.FormulaR1C1 = _
        "=VLOOKUP(RC1,'G:\TCI_PM\Port Rept & Perf Track\Daily Reprojections\TPV datafiles\[TPV datafile - 05-07-14.xlsx]TPV Data Pull'!C1:C88,36,FALSE)"

【问题讨论】:

  • 如果您可以说服您的组织从数字日期移到 dd-mmm-yy 以便始终清楚哪个月份,这将更容易。美国和英国有不同的约定(如您所知)。使用 dd-mmm-yy 可能成为已确定使用一种业务语言的组织的国际标准。

标签: vba excel


【解决方案1】:

这是一些用于解析在英国运行的美国日期的代码...

Sub TestParseUSDate()
    Debug.Assert VBA.FormatDateTime(ParseUSDate("05-07-14"), vbLongDate) = "07 May 2014"
End Sub

Function ParseUSDate(ByVal sUSADate As String) As Date

    Debug.Assert Len(sUSADate) >= 8

    Dim lMonth As Long
    lMonth = Left$(sUSADate, 2)

    Debug.Assert Not (IsNumeric(Mid$(sUSADate, 3, 1)))

    Dim lDay As Long
    lDay = Mid$(sUSADate, 4, 2)

    Debug.Assert Not (IsNumeric(Mid$(sUSADate, 6, 1)))

    Dim lYear As Long
    lYear = Mid$(sUSADate, 7)

    Dim dt As Date
    ParseUSDate = VBA.DateTime.DateSerial(lYear, lMonth, lDay)


End Function

【讨论】:

  • 我需要日期参考才能继续前进一天。我将代码修改为-
【解决方案2】:

我大部分都在工作,除了....我不确定如何结合路径和文件引用来正确设置公式。

公式应类似于“=VLOOKUP(RC1,'U:\00_Daily Reports["TPV datafile_2 - " & "myname" & ".xlsx"]TPV Data Pull'!C1:C88,36,FALSE)"

我现在的代码是:

Dim path As String
Dim file As String
Dim myformula As String
Dim myname As Date

myname = Day(Now() - 1)
path = "U:\00_Daily Reports\"
file = "TPV datafile_2 - " & "myname" & ".xlsx""

myformula = "-------------------------"
Range("CL2").Select
ActiveCell.FormulaR1C1 = myformula

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多