How to copy a every part of a worksheet

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
There is a worksheet inside a workbook located on server that I need to copy.
The problem that I am stuck on is how to copy all the contents of the page including the header content, footer content, and margin settings(Top,Left, Bottom, Footer,Right, and Header) of the selection using VBA. Here is what I have so far. Thank You for any help or advice offered.

Code:
Option ExplicitPrivate Sub Workbook_Open()
        
        Dim sPath As String, sFile As String
        Dim wb As Workbook
        
        sPath = "J:\GRP\EVERYONE\FORMS\"
        sFile = sPath & "F-103-04 Exh I-Solid Dose Usage Record-Rev001.xlsx"
        Set wb = Workbooks.Open(sFile)
        call OpenSDUsageRecord


Code:
Sub OpenSDUsageRecord()    Cells.Select
    With Selection
        .Copy
        
    End With
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I decided to just take the current Margin Values and assign them to their appropriate Page Margin value. Hope that make sense. Here's the updated code.
Code:
Option ExplicitPrivate Sub Workbook_Open()
        
        Dim sPath As String, sFile As String
        Dim wb As Workbook
        
        sPath = "J:\GRP\EVERYONE\FORMS\"
        sFile = sPath & "F-103-04 Exh I-Solid Dose Usage Record-Rev001.xlsx"
        Set wb = Workbooks.Open(sFile)
        Call OpenSDUsageRecord

The problem is located in the code below:

Code:
Sub OpenSDUsageRecord()    
    Dim NewWB As New Workbook
    
    Set NewWB = Application.Workbooks("ChattemPackaging.xlsm").Worksheets("Solid Dose Usage Record")
    ActiveWorkbook.Copy NewWB
        NewWB.TopMargin = 0.3
        NewWB.LeftMargin = 0.2
        NewWB.BottomMargin = 0.3
        NewWB.FooterMargin = 0.2
        NewWB.RightMargin = 0.2
        NewWB.HeaderMargin = 0.2
        
End Sub

After Editing this code, I now get a "Runtime Error '13: Type Mismatch" and the following line of code is highlighted in yellow.
Code:
Set NewWB = Application.Workbooks("ChattemPackaging.xlsm").Worksheets("Solid Dose Usage Record")
Sorry for the late edit. Thank You
 
Last edited:
Upvote 0
.
If you change :
Code:
[COLOR=#333333]Dim NewWB As New Workbook[/COLOR]
to
Code:
[COLOR=#333333]Dim NewWB As Workbook[/COLOR]

does the error go away ?
 
Upvote 0
Also NewWB in the line below
Code:
Set NewWB = Application.Workbooks("ChattemPackaging.xlsm").Worksheets("Solid Dose Usage Record")
is a Worksheet not a Workbook.
 
Upvote 0
Logit- When I changed my code to your suggestion and deleted the word new, I'm still getting a runtime error 13 type mismatch.

Mark858-Basically yes. I am copying the contents from one worksheet in another workbook to another specific worksheet in that workbook.

I really hope that makes sense and sorry for the poor explanation. Thank you both for your help.
 
Upvote 0
When I changed this line of code below, I now get "Runtime error '9: Subscript out of range"
Code:
Set NewWB = Worksheets("Solid Dose Usage Record")

Thank You
 
Upvote 0
You need to leave that line as it was originally but declare NewWB as Worksheet not Workbook or New Workbook.
 
Upvote 0
Thank You Mark858. That fixed that problem but I still don't know how to format the NewWB worksheet with Margin values listed in the original above code and the code below. Because now I'm getting "Compile error: Method or data member not found" Thank you for all your help.
Code:
Sub OpenSDUsageRecord()    
    Dim NewWB As Worksheet
    
    Set NewWB = Application.Workbooks("ChattemPackaging.xlsm").Worksheets("Solid Dose Usage Record")
    ActiveWorkbook.Copy NewWB
        NewWB[COLOR=#0000ff].TopMargin[/COLOR] [COLOR=#0000ff]= [/COLOR]0.3
        NewWB.LeftMargin = 0.2
        NewWB.BottomMargin = 0.3
        NewWB.FooterMargin = 0.2
        NewWB.RightMargin = 0.2
        NewWB.HeaderMargin = 0.2
End Sub
".TopMargin =" is highlighted in blue after compile error is displayed.
 
Last edited:
Upvote 0
Maybe...


Code:
    With NewWB.PageSetup
        .TopMargin = 0.3
        .LeftMargin = 0.2
        .BottomMargin = 0.3
        .FooterMargin = 0.2
        .RightMargin = 0.2
        .HeaderMargin = 0.2
    End With
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

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