Macro Copying Values, but not contents/formatting

Indestructible

New Member
Joined
Mar 18, 2011
Messages
4
Hi there, I'm using this code (borrowed liberally from the MSDN site) to copy a range of cells from multiple workbooks in a folder. It's working as intended, but I need it to copy formatting, cell contents, etc. At present it's only duplicating the values of the cells to the sheet. I understand why it's doing this, but I don't know enough (any) VBA to correct it. Any help someone could offer would be tremendous!

The problem in my limited understanding is using the "DestRange.Value = SourceRange.Value" method of copying, where I need to be using pastespecial I believe. I'm not sure how to implement though.


Code:
Sub MergeAllWorkbooks()
    Dim SummarySheet As Worksheet
    Dim FolderPath As String
    Dim NRow As Long
    Dim FileName As String
    Dim WorkBk As Workbook
    Dim SourceRange As Range
    Dim DestRange As Range
    
    ' Create a new workbook and set a variable to the first sheet.
    Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
    
    ' Modify this folder path to point to the files you want to use.
    FolderPath = "C:\Temp\"
    
    ' NRow keeps track of where to insert new rows in the destination workbook.
    NRow = 1
    
    ' Call Dir the first time, pointing it to all Excel files in the folder path.
    FileName = Dir(FolderPath & "*.xl*")
    
    ' Loop until Dir returns an empty string.
    Do While FileName <> ""
        ' Open a workbook in the folder
        Set WorkBk = Workbooks.Open(FolderPath & FileName)
        
        ' Set the cell in column A to be the file name.
        'SummarySheet.Range("A" & NRow).Value = FileName
        
        ' Set the source range to be B1 through X20.
        ' Modify this range for your workbooks.
        ' It can span multiple rows.
        Set SourceRange = WorkBk.Worksheets(1).Range("B1:X20")
        
        ' Set the destination range to start at column B and
        ' be the same size as the source range.
        Set DestRange = SummarySheet.Range("B" & NRow)
        Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
           SourceRange.Columns.Count)
           
        ' Copy over the values from the source to the destination.
        DestRange.Value = SourceRange.Value
        
        ' Increase NRow so that we know where to copy data next.
        NRow = NRow + DestRange.Rows.Count
        
        ' Close the source workbook without saving changes.
        WorkBk.Close savechanges:=False
        
        ' Use Dir to get the next file name.
        FileName = Dir()
    Loop
    
    ' Call AutoFit on the destination sheet so that all
    ' data is readable.
    SummarySheet.Columns.AutoFit
End Sub
 

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"
Try changing

Code:
DestRange.Value = SourceRange.Value

to

Code:
SourceRange.Copy Destination:=DestRange
 
Upvote 0
That's about 80% right. It's now throwing errors up because the sheets contain conditional formatting and cell objects with common names.
I have cell names like Gate 0, Gate 1, Gate 2 which flip conditional format colors on and off. I will show you how it should look, and how it does look

Correct:
abglz4.jpg



What I'm Getting:
2hydy8j.jpg


The Error it's spitting at me:
1495lw3.jpg


Your help is greatly appreciated!
 
Upvote 0
Not sure why the formatting isn't copying. To avoid the error try

Code:
Application.DisplayAlerts = False
SourceRange.Copy Destination:=DestRange
Application.DisplayAlerts = True
 
Upvote 0
That definitely suppressed the errors, but it still fails to copy the formatting or the contents of the cells.

I would even be happy with just capturing the selection in a HTML format and having it pasted into the new workbook as it is just an executive summary and doesn't need to be edited...
 
Upvote 0
Perhaps

Code:
Application.DisplayAlerts = False
SourceRange.Copy
DestRange.PasteSpecial Paste:=xlPasteValues
DestRange.PasteSpecial Paste:=xlPasteFormats
Application.DisplayAlerts = True
 
Upvote 0
Perhaps

Code:
Application.DisplayAlerts = False
SourceRange.Copy
DestRange.PasteSpecial Paste:=xlPasteValues
DestRange.PasteSpecial Paste:=xlPasteFormats
Application.DisplayAlerts = True


Better, but worse? Heh.
It now copies the values over for those cells that were coming up empty, but it scrubbed a tonne of the formatting for the rest of my stuff.

Should be:
2lc45mt.jpg


Is doing:
15yys00.jpg


Weird how it dropped the charts and stuff, where as it doesn't drop them with the simpler version you suggested before?!
 
Upvote 0
I'm using the same code (with this command: DestRange.Value = SourceRange.Value),
but the problem I've got is that when coping values from the source page, lets say there is 10 lines on that list and if there is an empty line in between, the code won't copy anything below empty line.
How to force the code to copy everything from the page regardless of empty lines or cells???
Could anybody help me with that please.
 
Upvote 0

Forum statistics

Threads
1,214,592
Messages
6,120,433
Members
448,961
Latest member
nzskater

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