PDA

View Full Version : [SOLVED] Copy Data / Paste Data with Value and Format



BenChod
06-01-2017, 05:18 AM
Hello All -

I am using the following code (credit to Ron de Bruin) to copy and paste data from different workbooks in a particular folder. The code copies the data range from the first worksheet in each workbook and pastes the data into the workbook that has the macro. This piece of code works super well. The only item that I can't figure out is how to paste the data with the format from the other worksheets. I am hoping someone can take a quick look at the code and provide guidance on how to copy the values along with the format. Thanks in advance for the help.


Sub MergeAllWorkbooks_2()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destrange As Range
Dim rnum As Long, CalcMode As Long
Dim FirstCell As String
'Fill in the path\folder where the files are
MyPath = "C:\Data\"
'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If
'Fill the array(myFiles)with the list of Excel files in the folder
FNum = 0
Do While FilesInPath <> ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop
'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
'Add a new workbook with one sheet
Set BaseWks = ThisWorkbook.Worksheets(2)
rnum = 1
'Loop through all files in the array(myFiles)
If FNum > 0 Then
For FNum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
On Error GoTo 0
If Not mybook Is Nothing Then
On Error Resume Next
With mybook.Worksheets(1)
FirstCell = "A2"
Set sourceRange = .Range(FirstCell & ":" & RDB_Last(3, .Cells))
'Test if the row of the last cell >= then the row of the FirstCell
If RDB_Last(1, .Cells) < .Range(FirstCell).Row Then
Set sourceRange = Nothing
End If
End With
If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0
If Not sourceRange Is Nothing Then
SourceRcount = sourceRange.Rows.Count
If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the sheet"
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else
'Copy the file name in column A
With sourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value = MyFiles(FNum)
End With
'Set the destrange
Set destrange = BaseWks.Range("B" & rnum)
'we copy the values from the sourceRange to the destrange
With sourceRange
Set destrange = destrange. _
Resize(.Rows.Count, .Columns.Count)
End With
destrange.Value = sourceRange.Value
rnum = rnum + SourceRcount
End If
End If
mybook.Close savechanges:=False
End If
Next FNum
BaseWks.Columns.AutoFit
End If
ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub

mdmackillop
06-01-2017, 06:17 AM
Please use code tags (# button) when posting code

mdmackillop
06-01-2017, 06:23 AM
Instead of

destrange.Value = SourceRange.Value


use

SourceRange.Copy
destrange.PasteSpecial xlValues
destrange.PasteSpecial xlFormats
Application.CutCopyMode = False

BenChod
06-01-2017, 06:29 AM
Please use code tags (# button) when posting code

Thank you again for your assistance. I will make sure to use the proper code tags.

BenChod
06-01-2017, 06:32 AM
One more item please. I want to remove .xlsx from the file name when it's being copied over. I used this and it works for files that have the .xlsx extension. For files with .xls, it's not being removed even when I added an asterisk.

With sourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value = Replace(MyFiles(FNum), ".xls*", "")

mdmackillop
06-01-2017, 06:49 AM
MyRange.Replace What:=".xls*", Replacement:="", LookAt:=xlPart

BenChod
06-01-2017, 07:13 AM
Thank you again. I added this at the end:

'Range("A").Replace What:=".xls*", Replacement:="", LookAt:=xlPart'

And I am getting a method range of object global failed.

mdmackillop
06-01-2017, 07:23 AM
That works if "A" is a named range, or do you mean Range("A:A")

BenChod
06-01-2017, 08:32 AM
Yeah, the range should be (A:A). Thank you again.