繁体   English   中英

VBA 将工作表从活动工作簿添加到新的 Excel 应用程序工作簿

[英]VBA Add Worksheet from active Workbook to new Excel Application Workbook

我尝试将工作表(“Pivot”)从一个工作簿复制到一个新工作簿。

        Private Sub button_start_Click()     'Start button in user form
        
        Dim app As New Excel.Application
        app.Visible = True
        Dim wb As Workbook
        Set wb = app.Workbooks.Add(input)    'import some Data via user form. This works.
        
        ThisWorkbook.Sheets("Pivot").Copy After:=wb.Sheets(wb.Sheets.Count)  


        End Sub 

最后一行不起作用。 我不确定两个工作簿之间的工作簿关系是否明确定义。

有人可以帮忙吗?

此致

  1. 输入字是保留字。

  2. 尽管没有必要,您仍然可以使用声明新应用程序的方法。

  3. 两种方法。 第一个有效,但效率不高,而且很丑陋。

     Private Sub button_start_Click() 'Start button in user form Dim app As New Excel.Application Dim wb As Workbook Dim sourceWb As Workbook Dim wbFullName as String, wbName as String app.Visible = True 'Change the textbox1 to what ever is the relevant to your 'situation (I assumed the value given is for an existing workbook and path) Set wb = app.Workbooks.Add(TextBox1) wb.save '(saveas) wbFullName = wb.FullName wbName = wb.Name wb.close 'The Workbooks collection doesn't register the newly opened book 'using this new app method.. So close it and reopen it set wb = Workbooks.Open filename:=wbFullName sourceWb.activate 'Make the source workbook active sourceWB.Sheets("Pivot").Copy After:=wb.Sheets(wb.Sheets.Count) End Sub

或者

你可以这样做

  Private Sub button_start_Click()     'Start button in user form
   Dim app As New Excel.Application
   Dim wb As Workbook
   Dim sourceWb As Workbook
    app.Visible = True        
    Set sourceWb = ThisWorkbook
    'Change the textbox1 to what ever is the relevant to your
    'situation (I assumed the value given is for an existing workbook and path

    Set wb = Workbooks.Add(TextBox1)
    sourceWb.Activate
    sourceWb.Sheets("Blank").Copy After:=Workbooks(wb.Name).Sheets(wb.Sheets.Count)
  End Sub

暂无
暂无

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

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