【发布时间】:2019-08-22 21:16:37
【问题描述】:
我有一张原始数据表,其中包括多天的车辆数量。每个日期都是一行,表示在 60 分钟内的车辆计数(因此每天 24 行)。 我有一个模型,它每天使用报告模板创建一个新工作表。我只是不知道如何获取每天的实际车辆计数数据来填充每小时的每张表。 创建的每个新选项卡都以日期命名。如果我们有 8 天的车辆计数,则将创建 8 个新选项卡。在该新选项卡中,我需要能够获取所有 24 辆汽车的数量并将它们粘贴到相应单元格中的模板报告中。
Option Explicit
Sub SheetsFromTemplate()
Dim wsMASTER As Worksheet, wsTEMP As Worksheet, wasVISIBLE As Boolean
Dim shDates As Range, Item As Range, NmStr As String
'keep focus in this workbook
With ThisWorkbook
'sheet to be copied
Set wsTEMP = .Sheets("Template")
'check if it's hidden or not
wasVISIBLE = (wsTEMP.Visible = xlSheetVisible)
'make it visible
If Not wasVISIBLE Then wsTEMP.Visible = xlSheetVisible
'sheet with dates and data
Set wsMASTER = .Sheets("Raw Data")
'range to find names to be checked
Set shDates = wsMASTER.Range("C9:C" & Rows.Count).SpecialCells(xlConstants)
Application.ScreenUpdating = False
'check one data at a time
For Each Item In shDates
NmStr = FixStringForSheetName(CStr(Item.Text))
'if sheet does not exist...
If Not Evaluate("ISREF('" & NmStr & "'!A1)") Then
'...create it from template
wsTEMP.Copy After:=.Sheets(.Sheets.Count)
'...rename it
ActiveSheet.Name = NmStr
End If
Next Item
'return to the master sheet
wsMASTER.Activate
'hide the template if necessary
If Not wasVISIBLE Then wsTEMP.Visible = xlSheetHidden
'update screen one time at the end
Application.ScreenUpdating = True
End With
MsgBox "All Reports created"
【问题讨论】: