I try to copy the worksheet ("Pivot") from one Workbook to a new one.
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
The last line is not working. I am not sure if the workbook relation between the two workbooks is clear defined.
Can someone help?
Best regards
The word input is a reserved word.
You can still use your method of declaring a new app although not necessary.
Two methods. This first one works but it is not efficient and is pretty ugly.
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
OR
You can do it this way
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.