使用主文件和来自多个文件的数据创建多个报告

时间:2019-01-03 11:40:35

标签: excel vba

我创建了一个Excel主报表,其中包含5个主要标签,其中3个包含多个枢轴,这些枢轴从3个单独的CSV文件中提取数据。 CSV文件没有单一的公分母,因此无法加入。我需要为不同的用户复制母版〜500次。每个用户只能看到他们自己的信息。有什么建议吗?

到目前为止,我想到的是在主文件中添加4个额外的标签-3个包含每个数据透视标签的数据,另外一个我用作控制室-由于我将文件分解为多个文件较小的组,并使用范围可变的名称。 我目前正在尝试编写以下代码:

  1. 保存主文件的单个副本
  2. 过滤CSV一次仅将一个用户复制一个数据到相应用户报告的相应数据标签中
  3. 刷新关键点
  4. 删除多余的标签

到目前为止,我仅设法完成了步骤1。

Sub SaveUserReports()
  Dim wb As Workbook
  Dim rNames As Range, c As Range, r As Range

  'Current file's list of names and ids on sheet1.
  Set rNames = Worksheets("Control_Room").Range("K8", Worksheets("Control_Room").Range("K8").End(xlDown))
  'Path and name to master workbook to open for copy, saveas.
  Set wb = Workbooks.Open(ThisWorkbook.Path & "\TestCopy2.xlsx")

  For Each c In rNames
    With wb
      .Worksheets("Control_Room").Range("k8").Value = c
      'Path and name for copied workbook
      .SaveAs filename:=ThisWorkbook.Path & "\UserReports\" & c.Value & ".xlsx", _
        FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    End With
    Set wb = ActiveWorkbook
  Next c
  wb.Close
End Sub

请问您可以执行第2-4步还是提出其他替代方案?我愿意接受任何可行的自动化解决方案。

对于第2-4步,我拥有(仅使用CSV之一,因为一旦上班就可以复制它):

Sub Import_Data()

    Application.ScreenUpdating = False

    Dim GBR_Code As Range
    Dim row As Long
    Dim col As Long
    Dim ImportString As String
    Dim wb As Workbook
    Dim destwb As Workbook
    Dim rNames As Range, c As Range
    Dim iCol As Long
    Dim lo As Range


    Set rNames = ThisWorkbook.Worksheets("Control_Room").Range("K8:K9")
    Set destwb = Workbooks.Open(ThisWorkbook.Path & "\" & "November GBR.xlsx")
    Set lo = destwb.Sheets("GBR_Data").UsedRange
    iCol = 1

      For Each c In rNames
        Set wb = Workbooks.Open(ThisWorkbook.Path & "\UserReports\" & c.Value & ".xlsx")
        wb.Worksheets("Control_Room").Range("k8").Value = c
        Set wb = ActiveWorkbook
        destwb.Sheets("GBR_Data").UsedRange.AutoFilter Field:=iCol, Criteria1:=c

        wb.Worksheets("GBR_Data").UsedRange.Clear

          'Copy GBR data from data workbook into report workbook
        destwb.Worksheets("GBR_Data").Range("A2:V100000").SpecialCells(xlCellTypeVisible).Copy wb.Sheets("GBR_Data").Range("A2")



NewWkb.Sheets("Overview").PivotTables("PivotTable1").RefreshTable

NewWkb.Sheets("Overview").PivotTables("PivotTable2").RefreshTable

NewWkb.Sheets("Overview").PivotTables("PivotTable3").RefreshTable

NewWkb.Sheets("Overview").PivotTables("PivotTable4").RefreshTable

NewWkb.Sheets("Overview").PivotTables("PivotTable5").RefreshTable

NewWkb.Sheets("Overview").PivotTables("PivotTable6").RefreshTable


          'Application.DisplayAlerts = False
          'NewWkb.Sheets("GBR_Data").Delete
          'Application.DisplayAlerts = True


          wb.Close SaveChanges = True

      Next c


End Sub

0 个答案:

没有答案