Is the first sheet in the first workbook blank?
Copy a range of cells from multiple worksheets in a folder , and paste them one after other in column wise for some range and row wise for other range with out replacing the existing data
- divya004
- Thread is marked as Resolved.
-
-
-
The variable lCol refers to the Master sheet in the destination workbook. That sheet will start out blank so the first value of lCol will be 2. After the column from the first source workbook is copied, it will always have data in row 1 and lCol will increase by 1 with each column copied. At the end, the macro deletes column A which remains blank.
-
Is the first sheet in the first workbook blank?
No it isn't
-
-
As I mentioned previously, I don't get any errors when I run the macro. This line of code:
is simply looking for the last row with data in Sheet(1) of each workbook, so unless the sheet is blank, I don't know why it's generating an error. Perhaps it's best to do what Dave has suggested.
It would be best to supply a sample workbook for both workbooks. The source workbook and the destination workbook.
-
I have attached the worksheets and highlighted what to be copied from source. Like this i have multiple files to be copied from but same format.
They can be copied in different sheets in same workbook destination.
-
Try:
Code
Display MoreSub CopyData() Application.ScreenUpdating = False Dim LastRow As Long, desWS1 As Worksheet, desWS2 As Worksheet, lCol As Long, FileToOpen As String, FileName As String Set desWS1 = ThisWorkbook.Sheets("Master") Application.Workbooks.Add 1 Set desWS2 = Sheets(1) FileToOpen = ThisWorkbook.Path & "\*.xlsx" FileName = Dir(FileToOpen) Do While FileName <> "" Workbooks.Open ThisWorkbook.Path & "\" & FileName With ActiveWorkbook With Sheets(1) LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row lCol = desWS1.Cells(1, desWS1.Columns.Count).End(xlToLeft).Column + 1 .Range("G5:G" & LastRow).Copy desWS1.Cells(1, lCol).PasteSpecial xlPasteValues .Range("I6:U6").Copy With desWS2 .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues End With End With .Close False End With FileName = Dir Loop desWS1.Columns(1).Delete Application.ScreenUpdating = True End Sub
-
-
Your original question and your sample workbooks do not match.
Here is the code that reflects on the sample workbook you have provided.
Your destination workbook is the workbook with the code and will be saved as a macro enabled workbook(.xlsm)
Change the folder address in the code.
There are two worksheets in the destination workbook. One named Columns and one named Rows.
Code
Display MoreSub LoopThroughFolder() Dim MyFile As String, Str As String, MyDir As String, wb As Workbook Dim rws As Long, rng1 As Range, rng2 As Range Dim sh1 As Worksheet, sh2 As Worksheet Dim Lstcl As Long, Lstrws As Long, cl As Long Set wb = ThisWorkbook Set sh1 = wb.Sheets("Columns") Set sh2 = wb.Sheets("Rows") 'change the address to suite MyDir = "C:\Users\Dave\SkyDrive\Documents\New folder" & "\" MyFile = Dir(MyDir & "*.xlsx") 'change file extension ChDir MyDir Application.ScreenUpdating = 0 Application.DisplayAlerts = 0 Do While MyFile <> "" Workbooks.Open (MyFile) With Worksheets(1) rws = .Cells(Rows.Count, "G").End(xlUp).Row cl = .Cells(6, .Columns.Count).End(xlToLeft).Column Set rng1 = .Range(.Cells(5, 7), .Cells(rws, 7)) Set rng2 = .Range(.Cells(6, 9), .Cells(6, cl)) With sh1 If .Cells(1, 1) = "" Then Lstcl = 1 Else Lstcl = .Cells(1, .Columns.Count).End(xlToLeft).Column + 1 End If End With With sh2 If .Cells(1, 1) = "" Then Lstrws = 1 Else Lstrws = .Cells(.Rows.Count, "A").End(xlUp).Row + 1 End If End With rng1.Copy sh1.Cells(1, Lstcl).PasteSpecial xlPasteValues rng2.Copy sh2.Cells(Lstrws, 1).PasteSpecial xlPasteValues Application.CutCopyMode = False End With ActiveWorkbook.Close True MyFile = Dir() Loop End Sub
-
Your original question and your sample workbooks do not match.
Here is the code that reflects on the sample workbook you have provided.
Your destination workbook is the workbook with the code and will be saved as a macro enabled workbook(.xlsm)
Change the folder address in the code.
There are two worksheets in the destination workbook. One named Columns and one named Rows.
Code
Display MoreSub LoopThroughFolder() Dim MyFile As String, Str As String, MyDir As String, wb As Workbook Dim rws As Long, rng1 As Range, rng2 As Range Dim sh1 As Worksheet, sh2 As Worksheet Dim Lstcl As Long, Lstrws As Long, cl As Long Set wb = ThisWorkbook Set sh1 = wb.Sheets("Columns") Set sh2 = wb.Sheets("Rows") 'change the address to suite MyDir = "C:\Users\Dave\SkyDrive\Documents\New folder" & "\" MyFile = Dir(MyDir & "*.xlsx") 'change file extension ChDir MyDir Application.ScreenUpdating = 0 Application.DisplayAlerts = 0 Do While MyFile <> "" Workbooks.Open (MyFile) With Worksheets(1) rws = .Cells(Rows.Count, "G").End(xlUp).Row cl = .Cells(6, .Columns.Count).End(xlToLeft).Column Set rng1 = .Range(.Cells(5, 7), .Cells(rws, 7)) Set rng2 = .Range(.Cells(6, 9), .Cells(6, cl)) With sh1 If .Cells(1, 1) = "" Then Lstcl = 1 Else Lstcl = .Cells(1, .Columns.Count).End(xlToLeft).Column + 1 End If End With With sh2 If .Cells(1, 1) = "" Then Lstrws = 1 Else Lstrws = .Cells(.Rows.Count, "A").End(xlUp).Row + 1 End If End With rng1.Copy sh1.Cells(1, Lstcl).PasteSpecial xlPasteValues rng2.Copy sh2.Cells(Lstrws, 1).PasteSpecial xlPasteValues Application.CutCopyMode = False End With ActiveWorkbook.Close True MyFile = Dir() Loop End Sub
-
Thank you @Mumps for initiating and being through all the way . And putting your efforts to resolve the issue. As I was new to forum I had trouble explaining the problem may be .
Thank you @davesexcel for great comments and the LoopThroughFolder code has worked exactly the way i needed.
Thank you @jolivanes and @UncleStringer for barging in and helping .
-
Please mark thread as solved
Participate now!
Don’t have an account yet? Register yourself now and be a part of our community!