copy multiple sheets and past as values in a new workbook VBA

DavyJones90

Board Regular
Joined
Feb 23, 2022
Messages
62
Office Version
  1. 365
Platform
  1. Windows
Hey there,
I don't get exactly what I need to change to make this code work. I need two worksheets to be copied to a new workbook and pasted as values. This is my code.


VBA Code:
Public Sub Modelo()

    Dim newWorkbook As Workbook
    Dim newWbPath As String: newWbPath = ThisWorkbook.Path & "\MODELOMNI DATA.xlsx"
    Set newWorkbook = Workbooks.Add
  

  
    ThisWorkbook.Worksheets("AUTO ENTRY POINTS").Range("O4").NumberFormat = "dd/mm/yy"
    ThisWorkbook.Worksheets("AUTO ENTRY POINTS").Range("O4").Value = Date
    ThisWorkbook.Worksheets("AUTO ENTRY POINTS").Cells.Copy
    newWorkbook.Worksheets(1).Cells.PasteSpecial Paste:=xlPasteFormats
    newWorkbook.Worksheets(1).Cells.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    newWorkbook.Worksheets(1).Range("A:A").ClearContents
    newWorkbook.Worksheets(1).Name = "Values"
    ThisWorkbook.Worksheets("XL FILE").Cells.Copy
    newWorkbook.Worksheets(2).Cells.PasteSpecial Paste:=xlPasteFormats
    newWorkbook.Worksheets(2).Cells.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    newWorkbook.Worksheets(2).Name = "XL FILE"
 
    Application.CutCopyMode = False

    newWorkbook.SaveAs newWbPath
    newWorkbook.Close
  
  


End Sub


With the second Worksheet, I get the "out of range" error

Thanks in advance,

David
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I added this to my sheets and now it does add a sheet, but is also ads an empty sheet called "Sheet2" how do I get rid of it?

VBA Code:
newWorkbook.Sheets.Add After:=Sheets("Values")
    newWorkbook.Sheets.Add.Name = "XL FILE"
 
Upvote 0
Just remove the first of those two lines.
 
Upvote 0
Solution
Glad you sorted it & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,216,526
Messages
6,131,187
Members
449,631
Latest member
mehboobahmad

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