Combine copied data into a single worksheet - vba

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

Related

How can I copy data from specific worksheet of a workbook to another with the same worksheet name?

I have 7 productivity files which I need to copy the data from the sheet titled worktracker and paste these in the worktracker sheet in the masterfile, but I'm getting:
Run-time error 1004
Method Range of object_Worksheet failed.
Private Sub CommandButton1_Click()
Dim file As String
Dim myPath As String
Dim wb As Workbook
Dim rng As Range
Dim lastrow As Long, lastcolumn As Long
Dim wbMaster As Workbook
Set wbMaster = Workbooks("WorkTracker_Master.xlsm")
Set rng = wbMaster.Sheets("WorkTracker").Range("A4:W4")
myPath = "\\BMGSMP1012\GBDMC_Team$\GBDM_ContentManagement\+CM_Reports\Productivity Reports\FY18\"
file = Dir(myPath & "*.xls*")
While (file <> "")
Set wb = Workbooks.Open(myPath & file)
lastrow = wb.Worksheets("WorkTracker").Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = wb.Worksheets("WorkTracker").Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cell(2, 1)(lastrow, lastcolumn)).Copy
Application.DisplayAlerts = False
ActiveWorkbook.Close
erow = WorkTracker.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination = Worksheets("WorkTracker").Range(Cells(erow, 1), Cells(erow, 4))
wb.Close SaveChanges:=True
Set wb = Nothing
file = Dir
Wend
Application.CutCopyMode = False
End Sub
You need to fully qualify all your objects, a comfortable and easy way, is to seperate each Workbook by using a nested With statement.
Also, as #YowE3K already mentioned in the comments above, you have a syntax error when defining the copied Range.
Try the code below, inside your While (file <> "") loop, after you Set wb = Workbooks.Open(myPath & file) :
With wb.Worksheets("WorkTracker")
lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
lastcolumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
.Range(.Cells(2, 1), .Cells(lastrow, lastcolumn)).Copy
End With
With wbMaster.Worksheets("WorkTracker")
' get first empty row in column "A"
erow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
' paste in the first empty row
.Range("A" & erow).PasteSpecial
End With
wb.Close SaveChanges:=True
Set wb = Nothing

Excel VBA, paste from multiple files

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

Merging different worksheet into one sheet (only specified rows)

I Have multiple worksheets (like 24 in number!). I would like to merge it into single sheet. All the worksheets have similar structure with header.
Glitch: At the end of every worksheet there is one or two rows with data summary
I would like to omit those line and have continues data of all worksheets.
Here is a piece of code which I used to merge it. But it made multiple sheets in single excel file. Is it possible to add some code within this piece of code.
Thanks in advance!
Sub GetSheets()
Path = "C:\path"
Filename = Dir(Path & "*.XLSX")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub
What does following code do:
- Code will copy data from all the sheets of all .xlsx files in the specified folder assuming all files have same structure
- Data is copied to sheet name Output of active file
- Last row of each sheet is not copied assuming it contains data summary
- Header will be copied from the first copied sheet
- Code will not add sheets to current file
Sub GetSheets()
Dim path As String, fileName As String
Dim lastRow As Long, rowCntr As Long, lastColumn As Long
Dim outputWS As Worksheet
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'this is the sheet where all the data will be displyed
Set outputWS = ThisWorkbook.Sheets("Output")
rowCntr = 1
path = "C:\path" & "\"
fileName = Dir(path & "*.XLSX")
Do While fileName <> ""
Workbooks.Open fileName:=path & fileName, ReadOnly:=True
For Each ws In ActiveWorkbook.Sheets
If rowCntr = 1 Then
'get column count
lastColumn = ws.Cells(1, Columns.Count).End(xlToLeft).Column
'copy header
Range(outputWS.Cells(1, 1), outputWS.Cells(1, lastColumn)).Value = Range(ws.Cells(1, 1), ws.Cells(1, lastColumn)).Value
rowCntr = rowCntr + 1
End If
'get last row with data of each sheet
lastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
'copy data from each sheet to Output sheet
Range(outputWS.Cells(rowCntr, 1), outputWS.Cells(rowCntr + lastRow - 3, lastColumn)).Value = Range(ws.Cells(2, 1), ws.Cells(lastRow - 1, lastColumn)).Value
rowCntr = rowCntr + lastRow - 2
Next ws
Workbooks(fileName).Close
fileName = Dir()
Loop
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
Following code may be useful for combining sheets.
This will ask to browse for file to combine. Then it will combine all sheets into one sheet named "Combine"
Sub Combine()
Dim openfile As String
MsgBox "Pls select Input file", vbOKOnly
openfile = Application.GetOpenFilename(FileFilter:="Excel Files (*.xlsx), *.xlsx", Title:="Select File To Be Opened")
Workbooks.OpenText (openfile)
Dim J As Integer
On Error Resume Next
Sheets(1).Select
Worksheets.Add
Sheets(1).Name = "Combined"
Sheets(2).Activate
Range("A1").EntireRow.Select
Selection.Copy Destination:=Sheets(1).Range("A1")
For J = 2 To Sheets.Count
Sheets(J).Activate
Range("A1").Select
Selection.CurrentRegion.Select
Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select
Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)
Next
Sheets(1).Select
End Sub
Once you've got them all into your active workbook you could do an additional step to put them on the same sheet.
Not knowing the layout of your data its difficult but if I assume there's always something in A1 and it's all in a large block then you could loop through the sheets and copy something like:
Dim i as integer
For i = 1 to ActiveWorkbook.Sheets.Count
Sheets(i).Range("A1").CurrentRegion.Copy
'Paste it into the sheet here below what's already there
Next i

Copy-paste special VBA does not work

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

I'm trying to copy a range of data from one worksheet to another

When I tried to copy sheet1 of workbook1 to sheet2 of workbook2 it doesn't show anything on sheet2 except the copy selection frame. If I comment out Activeworkbook.Close. the workbook1 is open and the adequate sheet as well. But not only it is not copying, but when I press control paste it actually paste the sheet2 to itself. So I tried Range(cells(2,1), Cells(lastrow,lastcolumn)).Select without closing the workbook I want to copy to verify if it would at least select the range needed but it returns me a runt time error '1004' . If anyone see where my mistake is please help. thank you
Dim directory As String, FileName As String
Dim lastrow As Long, lastcolumn As Long
Dim erow As Long
Application.ScreenUpdating = False
'Application.DisplayAlerts = False
directory = "C:\Users\Documents\file1\"
FileName = Dir(directory & "*.xl??")
'Open the Excel file
Workbooks.Open (directory & FileName)
'Select the worksheet and range of the worksheet to be copied
Worksheets("Luminaire Summary").Select
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
'ActiveWorkbook.Close
erow = Sheet7.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Sheet2").Range(Cells(2, 1), Cells(lastrow, lastcolumn))
I tried writing a shorter code after some advice but I'm still not getting any copied information from workbook1 sheet1 to workbook2 sheet2. Here is my last attempt with no success
Private Sub CommandButton1_Click()
Dim directory As String, FileName As String, total As Integer
Dim lastrow As Long, lastcolumn As Long
directory = "C:\Users\Documents\"
FileName = Dir(directory & "*.xl??")
Workbooks.Open (directory & FileName)
Worksheets("Luminaire Summary").Select
UsedRange.Copy
ActiveWorkbook.Close
Sheets("Sheet2").Range("A1").PasteSpecial
You could simplify by using UsedRange
Worksheets("Luminaire Summary").Select
UsedRange.Copy
Sheets("Sheet2").Range("A1").PasteSpecial <-- 'or however you're determiing which cell to paste to