【问题标题】:Automatically Generated Workbook Using Current Date, Month And Year使用当前日期、月份和年份自动生成的工作簿
【发布时间】:2017-07-20 08:46:00
【问题描述】:

我是 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 月工作表上显示的默认结构吗?即一个日期的数据为 5 行,下一个日期插入另外 5 行,依此类推?您应该在当前布局中缺少的工作表上还有一个日期列。
  • 几天不运行代码,然后运行代码,你想发生什么?
  • @jsotola。我希望excel每天自动添加行。如果月份发生变化,它将创建新工作表并从第一天开始,但公式与默认工作表相同。如果年份发生变化,它应该使用默认公式创建新的工作簿。例如(现在是 2017 年,工作簿名称应该是 2017 年,我们应该从 1 月到 7 月有工作表,然后 7 月有 24 作为日期。7 月的日期是 1-31,那么我们应该看到的 7 月工作表的最后日期应该是1-31 加所有的 SUM 行。明年是 2018 年它会自动创建新的工作簿 2018 然后从一月开始。
  • 是的,即使我没有运行代码。如果您错过打开它,我应该自动创建新行或工作表。如果你错过了一年打开它,你应该看到新的工作簿。 @jsotola

标签: vba excel auto-generate


【解决方案1】:

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

我要离开几天......回来后会想出更多的东西

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

【讨论】:

  • 不错的代码。但是用户有可能使用您的代码复制日期。
猜你喜欢
  • 2013-02-15
  • 1970-01-01
  • 1970-01-01
  • 2015-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多