使用当前日期,月份和年份自动生成的工作簿

时间:2017-07-20 08:46:01

标签: excel vba excel-vba auto-generate

我是MS EXCEL VBA的新手,但我还是读了一些关于VBA及其运作方式的博客。我知道重命名excel表,禁用行和添加新表。但我想知道     如何每天自动插入行,每月添加新工作表,每年创建新的相同(默认结构)工作簿添加MS EXCEL 2013?我怎么做?

今天的日期是1,然后明天excel会自动添加1行日期。如果1月份结束,则excel会自动添加2月份的新工作表。并且它的年份变化excel与当前年份生成相同的默认或空工作簿。

  

excel的默认结构,excel文件位于   https://github.com/Ailyn09/project102/blob/master/2017.xlsx

当前代码

Sub Sample()
        'Disable adding row

        Dim I As Integer
        Dim cbStr As String
        Dim cbCtrl As CommandBarControl
        Application.ScreenUpdating = False
        For I = 1 To 2
            If I = 1 Then
                cbStr = "row"
            Else
                cbStr = "column"
            End If
            For Each cbCtrl In Application.CommandBars(cbStr).Controls
                If cbCtrl.ID = 3183 Then
                    cbCtrl.Enabled = False
                End If
            Next
        Next
        Application.ScreenUpdating = True

        'Copy Existing Sheet And Month As Name 
        ActiveSheet.Copy , Sheets(Sheets.Count)
        ActiveSheet.Name = Month() 
    End Sub

这里每个月都有一个想法: https://excel.tips.net/T002017_Sheets_for_Months.html

1 个答案:

答案 0 :(得分:0)

这是一个开始...只是尝试一些代码...创建月工作表如果它不存在...然后将数据添加到工作表

我必须离开几天......当我回来时会想出更多的东西

Sub testDate()

    Debug.Print Format(Now(), "d")
    Debug.Print Format(Now(), "dd")
    Debug.Print Format(Now(), "ddd")
    Debug.Print Format(Now(), "m")
    Debug.Print Format(Now(), "mm")
    Debug.Print Format(Now(), "mmm")
    Debug.Print Format(Now(), "mmmm")
    Debug.Print Format(Now(), "yy")
    Debug.Print Format(Now(), "yyyy")

    Debug.Print Month(Now)
    Debug.Print MonthName(Month(Now))

    Dim ws As Sheets
    Set ws = ActiveWorkbook.Worksheets

    Dim nam As String
    nam = Format(Now(), "mmmm")                     ' month name in local language

    Dim sh As Worksheet
    If Evaluate("ISREF('" & nam & "'!A1)") Then     ' sheet name exists ?
        Set sh = ws(nam)
    Else
        Set sh = ws.Add(after:=ws(ws.Count))
        sh.Name = nam
    End If

' !!!!! use one of the sections below, but NOT both !!!!!
' --------------------------------------------------------------------------------------
    Dim lastCell As Range
    Set lastCell = sh.Range("A" & sh.Rows.Count).End(xlUp)                   ' last used cell in column A
    lastCell.Offset(1).Value = Format(Now(), "to\da\y i\s t\he dd of mmmm")  ' some of the characters must be escaped
' --------------------------------------------------------------------------------------
'   this section corrupts the lastCell value that is used above

'   if fixed number of rows per day, then put daily data in particular rows

'   Dim day As Integer
'   day = Format(Now(), "d")
'   sh.Range("A1").Offset(day).Value = Format(Now(), "to\da\y i\s t\he dd of mmmm")
' --------------------------------------------------------------------------------------

End Sub