I am trying to copy-paste special values and formatting from multiple workbooks into a master spreadsheet. The number of columns is the same in the source and destination and the number of rows varies.
I read the other threads on this and tried multiple ways (including defining a range in the destination file) but I still cannot make the paste special work. Below is my code:
Sub mergefiles()
Dim folderpath As String
Dim filepath As String
Dim filename As String
Dim erow As Long
folderpath = "D:\Test\"
filepath = folderpath & "*.xls*"
filename = Dir(filepath)
Dim lastrow As Long, lastcolumn As Long
Do While filename <> ""
Workbooks.Open (folderpath & filename)
Worksheets("EmplOffers").ShowAllData
lastrow = ActiveSheet.Cells(Rows.Count, 3).End(xlUp).Row
lastcolumn = ActiveSheet.Cells(6, Columns.Count).End(xlToLeft).Column
Range(Cells(6, 3), Cells(lastrow, lastcolumn)).Copy
Application.DisplayAlerts = False
ActiveWorkbook.Close
erow = Worksheets("EmplOffers").Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Row
Worksheets("EmplOffers").Range(Cells(erow, 3), Cells(erow, 20)).PasteSpecial=:xlPasteValuesAndNumberFormats
filename = Dir
Loop
Application.DisplayAlerts = True
End Sub
Thanks a lot!
Try adjusting the paste call:
Worksheets("EmplOffers").Range(Cells(erow, 3).Address).PasteSpecial Operation:=xlPasteValuesAndNumberFormats
A named parameter should precede :=
Change
Worksheets("EmplOffers").Range(Cells(erow, 3), Cells(erow, 20)).PasteSpecial=:xlPasteValuesAndNumberFormats
To:
Worksheets("EmplOffers").Range(Cells(erow, 3), Cells(erow, 20)).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Related
I have following issues with this code.
it wont run when i open excel.
And
It will not paste from my files correctly. i want it to step to the last row and paste my info, then step down and paste from the second file, and so on.
any ideas?
Private Sub Workbook_Open()
Dim FolderPath As String
Dim FileName As String
FolderPath = "D:\excelprojekt\"
FileName = Dir(FolderPath & "*.xlsx")
Dim lastrow As Long
Dim lastcolumn As Long
Do While FileName <> ""
Workbooks.Open (FolderPath & FileName)
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(1, 1), Cells(lastrow, lastcolumn)).Copy
Application.DisplayAlerts = False
ActiveWorkbook.Close
With ActiveSheet
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
ActiveCell.Offset(rowOffset:=2, columnOffset:=0).Activate
ActiveSheet.PasteSpecial
End With
FileName = Dir
Loop
End Sub
I think it's possible to maintain copied data after closing a workbook, but there's no reason to do that here. If you qualify your workbook references you can copy from one workbook to another while both are open. If you know what sheets you want to be copying from and into, you should probably explicitly reference them instead of using ActiveSheet as well (I think ActiveSheet will be whatever sheet was active when the file was last saved when opening a file)
Private Sub Workbook_Open()
Dim FolderPath As String
Dim FileName As String
FolderPath = "D:\excelprojekt\"
FileName = Dir(FolderPath & "*.xlsx")
Dim lastrow As Long
Dim lastcolumn As Long
Dim wbOpened as Workbook
Do While FileName <> ""
Set wbOpened = Workbooks.Open(FolderPath & FileName)
With wbOpened.ActiveSheet
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = .Cells(1, Columns.Count).End(xlToLeft).Column
.Range(.Cells(1, 1), .Cells(lastrow, lastcolumn)).Copy
End With
ThisWorkbook.ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(2).PasteSpecial
Application.DisplayAlerts = False
wbOpened.Close
FileName = Dir
Loop
End Sub
I'm new to VBA. I found this code on a youtube video to copy data from Multiple Workbooks into Master, which is what I want to do. However, I get a compile error: Invalid or unqualified reference when it gets to "*.xlsx" It does not like the period: Help gives me this: An identifier beginning with a period is valid only within a With block. This error has the following cause and solution:
The identifier begins with a period.
Complete the qualification of the identifier or remove the period.
Any suggestions?
Sub copyDataFromMultipleWorkbooksIntoMaster()
Dim FolderPath As String, Filepath As String, Filename As String
FolderPath = "G:\control_chart\"
Filepath = FolderPath & “ * .xlsx”
Filename = Dir(Filepath)
Dim lastrow As Long, lastcolumn As Long
Do While Filename <> “”
Workbooks.Open (FolderPath & Filename)
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(2, 1), Cells(lastrow, lastcolumn)).Copy
Application.DisplayAlerts = False
ActiveWorkbook.Close
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
ActiveSheet.Paste Destination:=Worksheets(“Sheet1”).Range(Cells(erow, 1), Cells(erow, lastcolumn))
ActiveSheet.Paste Destination:=Worksheets(“Sheet1”).Range(Cells(erow, 1), Cells(erow, 4))
Filename = Dir
Loop
End Sub
I am currently use a VBA script to combine data from multiple sheets and workbooks into a a new workbook. Currently the script does this but creates multiple sheets in the destination workbook. Is it possible to just has destination be a single sheet?
Sub copydata()
Dim FolderPath As String, FilePath As String, FileName As String
FolderPath = "C:\attach\"
FilePath = FolderPath & "*.xlsx"
FileName = Dir(FilePath)
Dim erow As Long, lastrow As Long, lastcolumn As Long
'loops through directory as long as it is not blank and defines files as workbooks.
Do While FileName <> ""
Dim wb As Workbook
Set wb = Workbooks.Open(FolderPath & FileName)
'nested loop for sheets in workbooks
For counter = 3 To 9
'Sheets(“Sheet1”).Select
wb.Worksheets(counter).Activate
lastrow = ActiveSheet.Cells(Rows.count, 1).End(xlUp).Row
lastcolumn = ActiveSheet.Cells(1, Columns.count).End(xlToLeft).Column
Range(Cells(2, 1), Cells(lastrow, lastcolumn)).Copy
'Sheets("Sheet1").Select
Workbooks("ZMasterFile.xlsx").Worksheets(counter).Activate
erow = ActiveSheet.Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
Cells(erow, 1).Select
ActiveSheet.Paste
Next
wb.Close savechanges:=False
FileName = Dir
Loop
erow = ActiveSheet.Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
Cells(erow, 1).Select
This line might causing you the trouble.
'Sheets("Sheet1").Select
Workbooks("ZMasterFile.xlsx").Worksheets(counter).Activate
On the secound line, you are activating worksheet(counter)
I think you should put Worksheet("Sheet1") if you want all the datas to be in same sheet.
Of course. Probably your best bet is to install the AddIn you find from the link below.
http://www.rondebruin.nl/win/addins/rdbmerge.htm
That tool is very useful! Try it and you'll see!
Also, see the link below. You will see all the code exposed there, so it's a great learning example.
http://www.rondebruin.nl/win/s3/win008.htm
The code is working fine but copying data from the active sheet not Sheet3 of workbooks kindly if any one guide me i replaced active sheet with sheet3 but that doesn't work either.
Sub copyDataFromMultipleWorkbooksIntoMaster()
Dim FolderPath As String, Filepath As String, Filename As String
FolderPath = "D:\Copy Multiple Excel to One master\"
Filepath = FolderPath & "*.xls*"
Filename = Dir(Filepath)
Dim LastRow As Long, lastcolumn As Long
Do While Filename <> ""
Workbooks.Open (FolderPath & Filename)
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(5, 1), Cells(LastRow, lastcolumn)).Copy
Application.DisplayAlerts = False
ActiveWorkbook.Close
erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, 1))
Filename = Dir
Loop
Application.DisplayAlerts = True
End Sub
Public Function ModDate()
ModDate = Format(FileDateTime(ThisWorkbook.FullName), "m/d/yy h:n ampm")
End Function
Sub copyDataFromMultipleWorkbooksIntoMaster()
Dim FolderPath As String, Filepath As String, Filename As String
FolderPath = "D:\Copy Multiple Excel to One master\"
Filepath = FolderPath & "*.xls*"
Dim lastRow As Long, lastCol As Long, eRow As Long
Dim wb As Workbook, ws As Worksheet
Application.DisplayAlerts = False
Filename = Dir(Filepath)
Do While Filename <> ""
eRow = Sheet1.Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
Set wb = Workbooks.Open(FolderPath & Filename)
On Error Goto NextFile
Set ws = wb.Worksheets("Sheet3")
With ws
lastRow = .Cells(.Rows.count, 1).End(xlUp).Row
lastCol = .Cells(1, .Columns.count).End(xlToLeft).Column
.Range(.Cells(5, 1), .Cells(lastRow, lastCol)).Copy
Sheet1.Cells(eRow, 1).PasteSpecial xlPasteValues
End With
NextFile:
On Error Goto 0
wb.Close False
Filename = Dir
Loop
Application.DisplayAlerts = True
End Sub
Code works fine as it imports data from sheets of different workbooks with name Trippings_15.
But i want the program to import sheets with name Trippings_Jan_15, Trippings_Feb_15, Trippings_March_15, etc from workbook 1,2,3 respectively when i use Trippings_15 in code or I can simply give the absolute address of that sheet irrespective of tab name like sheet7 from all workbooks.
I am making a database where all monthly trippings of 2015 will be shown a single sheet.
Sub copyDataFromMultipleWorkbooksIntoMaster()
Dim FolderPath As String, Filepath As String, Filename As String
FolderPath = "D:\Copy Multiple Excel to One master\"
Filepath = FolderPath & "*.xls*"
Dim lastRow As Long, lastCol As Long, eRow As Long
Dim wb As Workbook, ws As Worksheet
Application.DisplayAlerts = False
Filename = Dir(Filepath)
Do While Filename <> ""
eRow = Sheet1.Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
Set wb = Workbooks.Open(FolderPath & Filename)
On Error Goto NextFile
Set ws = wb.Worksheets("Trippings_15")
With ws
lastRow = .Cells(.Rows.count, 1).End(xlUp).Row
lastCol = .Cells(1, .Columns.count).End(xlToLeft).Column
.Range(.Cells(5, 1), .Cells(lastRow, lastCol)).Copy
Sheet1.Cells(eRow, 1).PasteSpecial xlPasteValues
End With
NextFile:
On Error Goto 0
wb.Close False
Filename = Dir
Loop
Application.DisplayAlerts = True
End Sub
Try this. The logic here is that you predefine the months which you will insert in the "Trippings_15" string. Also, add a function to test whether sheet exists, instead of using the clunky On Error Resume Next
Sub copyDataFromMultipleWorkbooksIntoMaster()
Dim FolderPath As String, Filepath As String, Filename As String
'### DEFINE YOUR BASE STRING TO BE UPDATED WITH EACH MONTH
Dim baseSheetName$
baseSheetName = "Trippings_{}_15"
Dim sheetName as String 'This will be updated later...
'### DEFINE AN ARRAY OF MONTHS
Dim months, m
months = Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC")
FolderPath = "D:\Copy Multiple Excel to One master\"
Filepath = FolderPath & "*.xls*"
Dim lastRow As Long, lastCol As Long, eRow As Long
Dim wb As Workbook, ws As Worksheet
Application.DisplayAlerts = False
Filename = Dir(Filepath)
Do While Filename <> ""
eRow = Sheet1.Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
Set wb = Workbooks.Open(FolderPath & Filename)
For Each m in months '## Iterate over each month in your array
sheetName = Replace(baseSheetName,"{}",m) '## this is the month sheet name like "Trippings_Jan_15", etc.
If SheetExists(wb, sheetName) Then '## Check whether this sheet exists before tryingto use it
Set ws = wb.Worksheets(sheetName)
With ws
lastRow = .Cells(.Rows.count, 1).End(xlUp).Row
lastCol = .Cells(1, .Columns.count).End(xlToLeft).Column
.Range(.Cells(5, 1), .Cells(lastRow, lastCol)).Copy
Sheet1.Cells(eRow, 1).PasteSpecial xlPasteValues
End With
End If
Next m
wb.Close False
Filename = Dir
Loop
Application.DisplayAlerts = True
End Sub
Here is the function SheetExists:
Function SheetExists(wb as Workbook, s as String)
Dim ws as Worksheet
Dim ret as Boolean
For Each ws in wb.Worksheets
If ws.Name = s Then
ret = True
Exit For
End If
Next
SheetExists = ret
End Function