VBA:根据工作簿名称确定日期

时间:2016-02-12 15:44:07

标签: excel vba excel-vba

我有一个工作表,每个月都会发送一个名为MONTH YEAR Report的工作表(例如January 2016 Report

报告包含工作表中的所有必要信息,但月份和年份除外。有没有办法根据工作簿的名称将月份和年份拉到列中?

3 个答案:

答案 0 :(得分:3)

如果保留文件名的格式,您可以尝试从给定的文件名中获取月份和年份。

 Sub MonAndYear()
    Dim MyNAme As String
    Dim Cet 
    MyName = ThisWorkbook.Name
    Cet =Split(MyName," ")
    Dim Mon As String
    Dim Yr As String
    Mon = Cet(0)
    Yr = Cet(1)

     MsgBox "Month is " & Cet(0)
     MsgBox "Year is " & Cet(1)


End Sub

答案 1 :(得分:1)

无论名称结构如何,实现此目的的另一种方法

Sub GetMonthAndYear()
    Dim i As Integer, yr As Integer
    Dim na As String, mnth As String
    Dim arr

    na = ThisWorkbook.Name

    For i = 1 To 12
        If InStr(1, na, MonthName(i, True)) > 0 Then
            mnth = MonthName(i)
            Exit For
        End If
    Next i

    arr = Split(na, " ")

    On Error Resume Next
    yr = Join(Filter(arr, 19), "")
    yr = Join(Filter(arr, 20), "")
    On Error GoTo 0

    If yr = 0 Then
        MsgBox "Can't find year of report for " & na
    Else
        MsgBox "Month of report is: " & mnth & vbNewLine & "Year of report is: " & yr
    End If
End Sub

答案 2 :(得分:1)

You can have this formula put into the workbook which gets carried forward monthly:

[Object]
id = [view].Id
property1 = [view].property1
property2 = [view].property2
propertyN = [view].propertyN
Collection = [{property1 = [view].complex_type_collection_property1, ...}, ...]

This looks at the workbook's own filename, and pulls every character in between the "[" & "]" brackets. That is, it doesn't include the filepath and sheet name.