Modify code to export a specific sheet when creating a workbook

Neltu

New Member
Joined
Jan 28, 2009
Messages
25
Hi,

I currently have code that will create a new workbook, and paste data onto the new workbook after a macro is run. What i want to do is make sure that Sheet 1 in the original document is always included when the workbook is created.
Code is as follows:
Code:
Sub NewDocCreate()
Application.ScreenUpdating = False
Dim xcelNew As Excel.Application
Dim xcelSheet As Excel.Workbook
CurWkbk = ActiveWorkbook.Name
Set xcelNew = CreateObject("Excel.Application")
Windows(CurWkbk).Activate
Sheets("Checklist").Activate
ActiveSheet.Cells.Activate
ActiveSheet.Cells.Copy
Set xcelSheet = xcelNew.Workbooks.Add
xcelSheet.SaveAs ("MCRC1_" & Environ("UserName") & ".xls")
xcelNew.DisplayAlerts = False
xcelSheet.Close
Set xcelSheet = Workbooks.Open(Filename:="MCRC1_" & Environ("UserName") & ".xls")
Workbooks("MCRC1_" & Environ("UserName") & ".xls").Activate
Sheets("Sheet2").Cells.PasteSpecial
Application.CutCopyMode = False
xcelSheet.Save
xcelSheet.Close
MsgBox "Test Script exported to " & ("C:\Documents and Settings" & Application.PathSeparator & Environ("UserName") & Application.PathSeparator & "My Documents" & Application.PathSeparator & "MCRC1_" & Environ("UserName") & ".xls"), vbInformation, "Export Success"
Set xcelNew = Nothing
Set xcelSheet = Nothing
End Sub

I have already modified it so that it pastes the result into Sheet 2, but i cant figure out how to make it include Sheet 1 titled "Instructions" from the Current workbook whenever it creates the new workbook for the data to be pasted in.

Any help would be greatly appreciated.

Thanks in advance.
 
Last edited:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
You can just copy the entire sheet(s) to a new workbook:

Code:
Sub NewDocCreate()
Dim wbNew As Workbook, strWbName As String

Application.ScreenUpdating = False

'create new workbook, set to variable
Set wbNew = Workbooks.Add

With wbNew
    'copy "Instructions" and "Checklist" sheets from current workbook to new workbook
    ThisWorkbook.Sheets(Array("Instructions", "Checklist")).Copy Before:=wbNew.Sheets(1)
    
    'delete Sheet1 and Sheet2 from new workbook
    Application.DisplayAlerts = False
    .Sheets(Array("Sheet1", "Sheet2")).Delete
    Application.DisplayAlerts = True
    
    'save new workbook
    .SaveAs ("MCRC1_" & Environ("UserName") & ".xls")
    
    'set the full path of new workbook-including filename-to variable
    strWbName = .FullName
    
    'close new workbook
    .Close (False)
End With

'display msgbox
MsgBox "Test Script exported to " & strWbName, vbInformation, "Export Success"

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,409
Messages
6,119,339
Members
448,888
Latest member
Arle8907

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top