A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Dennis,
Try this file also uploaded to my MediaFire account:
http://www.mediafire.com/?2odd15g05pb61bg
It's named for_djo1212_Copy1Book.xlsm
There are 2 buttons on the sheet that both do very much the same thing: allow you to select a workbook and then copy some sheets from it into the workbook. The difference in the two routines is the way that it copies the formatting from the source workbook. Hopefully one of the two ways will copy the colors properly.
You'll need to alter the code to provide the name of a couple of sheets in the workbook you're going to choose for the testing.
If one of these routines works for you, then we can base more code on that routine.
Here's the code for the routine I think is going to have the best chance of doing things the way you need them to be done.
Sub CopyWithSourceFormatting()
Dim fileChosen As String
Dim sourceWB As Workbook 'will be the Crystal Reports (CR) created workbook
Dim sourceWS As Worksheet 'will be a sheet from the sourceWB
Dim sourceData As Range ' will be used cells on sourceWS
Dim destWS As Worksheet ' will be the new sheet(s) added to this workbook.
fileChosen = Application.GetOpenFilename("Excel Files *.xls*, *.xls*", , "Select a File To Process")
If fileChosen = "False" Then
'user cancelled file selection
Exit Sub ' just quit
End If
'improve performance and keep screen flickering to a minimum
Application.ScreenUpdating = False
'keep from nagging user while opening the other file
Application.DisplayAlerts = False
Set sourceWB = Workbooks.Open(fileChosen, False, True) ' open, no link updates, read only
Application.DisplayAlerts = True
'the crystal reports workbook is active, make this one active again
ThisWorkbook.Activate
'
'we are going to copy the sheets from the Crystal Reports file
'one by one, copying the values first and then the formats to
'see if that helps with keeping the colors true
'
'just repeat this section of code as required for each of the sheets
'
'set a reference to the sheet in the CR workbook to be copied
'*************
Set sourceWS = sourceWB.Worksheets("Sheet1") ' change name as needed
'*************
Set sourceData = sourceWS.UsedRange
'now add a new sheet to this workbook that will be referenced as destWS
ThisWorkbook.Sheets.Add After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
Set destWS = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
sourceData.Copy
'first copy the values and special number formats
destWS.Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, _
skipBlanks:=False, Transpose:=False
'next copy the "source themes" formatting
destWS.Range("A1").PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, _
skipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
'
'repeated section for next sheet all you have to change is the name of sheet to copy from
'*************
Set sourceWS = sourceWB.Worksheets("Sheet2") ' change name as needed
'*************
Set sourceData = sourceWS.UsedRange
'now add a new sheet to this workbook that will be referenced as destWS
ThisWorkbook.Sheets.Add After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
Set destWS = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
sourceData.Copy
'first copy the values and special number formats
destWS.Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, _
skipBlanks:=False, Transpose:=False
'next copy the "source themes" formatting
destWS.Range("A1").PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, _
skipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
'close the CR workbook
sourceWB.Close False ' close source workbook without saving changes
'do final housekeeping cleanup
Set destWS = Nothing
Set sourceData = Nothing
Set sourceWS = Nothing
Set sourceWB = Nothing
End Sub