简体   繁体   English

通过循环创建具有多个工作表的工作簿

[英]Create a workbook with multiple sheets through a loop

I'm looping through a Scripting Dictionary and call a function that loads specific filtered and sorted datasets to a sheet named "DonneesFiltrees".我正在遍历脚本字典并调用 function 将特定的过滤和排序数据集加载到名为“DonneesFiltrees”的工作表中。

I want to create a single workbook and through this loop add Sheets to the new workbook and copy paste "DonneesFiltrees" dataset into each sheets.我想创建一个工作簿,并通过此循环将工作表添加到新工作簿并将“DonneesFiltrees”数据集复制粘贴到每个工作表中。

There is my code at this moment, my loop and function are working great but I have no clue about how to insert multiple sheet to a new workbook此刻有我的代码,我的循环和 function 运行良好,但我不知道如何将多个工作表插入新工作簿

Set wsData = Worksheets("DonneesFiltrees")
Application.ScreenUpdating = False
    
Dim varkey As Variant
    
'ActiveWorkbook.SaveAs (ThisWorkbook.Path & "\data_output\export_du_" & Format(Now(), "DD-MMM-YYYY hh mm AMPM") & ".xlsx")
    
For Each varkey In DicTheme.Keys

    Call ChargerLesDonnees(CStr(DicTheme(varkey)), Me.listEntreprise.Value)
        
    If wsData.Range("A2").Value <> "" Then
        
        'Create sheet into new Workbook,
        'Set DicTheme(varkey) as sheet name,
        'copy paste wsData sheet dataset into this new sheet.
            
    End If
        
Next varkey
    
Application.ScreenUpdating = True

Thank you in advance for your help.预先感谢您的帮助。

Please have a look at the modified code of yours请查看您的修改后的代码

Set wsData = Worksheets("DonneesFiltrees")
Application.ScreenUpdating = False
    
Dim varkey As Variant
    
'ActiveWorkbook.SaveAs (ThisWorkbook.Path & "\data_output\export_du_" & Format(Now(), "DD-MMM-YYYY hh mm AMPM") & ".xlsx")
    
' Create a new workbook
Dim wkb As Workbook
Set wkb = Workbooks.Add
    
For Each varkey In DicTheme.Keys

    Call ChargerLesDonnees(CStr(DicTheme(varkey)), Me.listEntreprise.Value)
        
    If wsData.Range("A2").Value <> "" Then
        
        'Create sheet into new Workbook,
        Dim wks As Worksheet        ' no harm if one puts the declartion in the loop
        Set wks = wkb.Worksheets.Add
        
        'Set DicTheme(varkey) as sheet name,
        wks.Name = DicTheme(varkey)  ' DicTheme(varkey) should be a valid sheet name
        
        'copy paste wsData sheet dataset into this new sheet.
            
            
    End If
        
Next varkey
    
Application.ScreenUpdating = True


Further reading进一步阅读
Workbooks Add 工作簿添加
Worsheets Add 工作表添加

Thanks a lot for bringing help, here is the final code in case it can interest people.非常感谢您提供帮助,这是最终代码,以防人们感兴趣。 As the workbook is created with a default worksheet I'm deleting it at the end before to set a name and save the file.由于工作簿是使用默认工作表创建的,因此我在最后删除它以设置名称并保存文件。

    Set wsData = Worksheets("DonneesFiltrees")
    Application.ScreenUpdating = False
    
    Dim varkey As Variant
              
    ' Create a new workbook
    Dim wkb As Workbook
    Set wkb = Workbooks.Add
        
    ThisWorkbook.Activate
    
    For Each varkey In DicTheme.Keys
    
        Call ChargerLesDonnees(CStr(DicTheme(varkey)), Me.listEntreprise.Value)
            
        If wsData.Range("A2").Value <> "" Then
            
            'Create sheet into new Workbook,
            Dim wks As Worksheet        ' no harm if one puts the declartion in the loop
            Set wks = wkb.Worksheets.Add
            
            'Set DicTheme(varkey) as sheet name,
            wks.Name = DicTheme(varkey)  ' DicTheme(varkey) should be a valid sheet name
            
            'copy paste wsData sheet dataset into this new sheet.
            wsData.Visible = xlSheetVisible
            wsData.ListObjects(1).HeaderRowRange.Copy Destination:=wks.Range("A1")
            wsData.ListObjects(1).DataBodyRange.Copy Destination:=wks.Range("A2")
            wsData.Visible = xlSheetHidden
                
        End If
            
    Next varkey
        
    Application.DisplayAlerts = False
    wkb.Sheets("Feuil1").Delete
    Application.DisplayAlerts = True
    wkb.SaveAs (ThisWorkbook.Path & "\data_output\export_GLOBAL_de_" & Me.listEntreprise.Value & "_du_" & Format(Now(), "DD-MMM-YYYY hh mm AMPM") & ".xlsx")
    wkb.Close
 
    ThisWorkbook.Activate
    
    wsData.Cells.Clear
    
    Application.ScreenUpdating = True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM