exporting multiple sheets to new workbook as values and source formatting

psg2002

New Member
Joined
Feb 14, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi,
I am able to run my VBA code smoothly. However, I am not sure how to paste them as values and keep the source formatting.

VBA Code:
Sub ExportWorksheets()
    
    Dim wbSource As Workbook, wbTarget As Workbook
    Dim worksheetList As String 
    Dim worksheetArr As Variant
    Dim arrIndx As Long


    worksheetList = "GOOGL:TSLA"
    
    worksheetArr = Split(worksheetList, ":")
    
    If UBound(worksheetArr) = -1 Then Exit Sub
    
    Set wbSource = ThisWorkbook
    
    Set wbTarget = Workbooks.Add
    
    For arrIndx = LBound(worksheetArr) To UBound(worksheetArr)
    ThisWorkbook.Worksheets(worksheetArr(arrIndx)).Copy wbTarget.Worksheets(wbTarget.Worksheets.Count)
    
    Next arrIndx
    
     MsgBox "Export complete.", vbInformation
    
CleanObjects:
    Set wbTarget = Nothing
    Set wbSource = Nothing


End Sub

I appreciate your help in advanced!
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I believe this reference will solve your problem

 
Upvote 0
I believe this reference will solve your problem

My code stops at the following red line :( not sure what i am doing wrong
Rich (BB code):
Sub ExportWorksheets()
   
    Dim wbSource As Workbook, wbTarget As Workbook
    Dim worksheetList As String
    Dim worksheetArr As Variant
    Dim arrIndx As Long


    worksheetList = "GOOGL:TSLA"
   
    worksheetArr = Split(worksheetList, ":")
   
    If UBound(worksheetArr) = -1 Then Exit Sub
   
    Set wbSource = ThisWorkbook
   
    Set wbTarget = Workbooks.Add
   
    For arrIndx = LBound(worksheetArr) To UBound(worksheetArr)
    ThisWorkbook.Worksheets(worksheetArr(arrIndx)).Copy wbTarget.Worksheets(wbTarget.Worksheets.Count)
    wbtarget.worksheets(worksheetarr(arrindx).PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, SkipBlanks:=False

    Next arrIndx
   
     MsgBox "Export complete.", vbInformation
   
CleanObjects:
    Set wbTarget = Nothing
    Set wbSource = Nothing


End Sub
 
Upvote 0
Looking at this section:

For arrIndx = LBound(worksheetArr) To UBound(worksheetArr)
ThisWorkbook.Worksheets(worksheetArr(arrIndx)).Copy wbTarget.Worksheets(wbTarget.Worksheets.Count)
wbtarget.worksheets(worksheetarr(arrindx).PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, SkipBlanks:=False

The Bold Red command is already paste destination. Then on the next line you were pasting again. The Bold RED line should not be there.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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