Copy data only from last used row from multiple excel workbooks in a folder and paste into master file

Reetesh

Board Regular
Joined
Sep 6, 2020
Messages
50
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
i'm trying to come up with a code which copies data from last rows of multiple workbooks in a folder and paste into another workbook(Master file). The below mentioned code copies the complete data present in all the workbooks in the folder and paste it into another workbook without any issues:

Sub LoopThroughFolder()

Dim MyFile As String, Str As String, MyDir As String, Wb As Workbook
Dim Rws As Long, Rng As Range
Set Wb = ThisWorkbook

MyDir = "C:\Users\Jeevesh\Desktop\Dump\New folder\"
MyFile = Dir(MyDir & "*.xls")
ChDir MyDir
Application.ScreenUpdating = 0
Application.DisplayAlerts = 0

Do While MyFile <> ""
Workbooks.Open (MyFile)
With Worksheets("Activity Data")
Rws = .Cells(Rows.Count, "B").End(xlUp).Row
Set Rng = Range(.Cells(2, 1), .Cells(Rws, 9))
Rng.Copy Wb.Worksheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Offset(1, -1)
ActiveWorkbook.Close True
End With
Application.DisplayAlerts = 1
MyFile = Dir()
Loop

End Sub

Now i'm not able to make changes to the code which will only copy the last used row from all the workbooks present in the folder and paste it into another workbook.
 
Hello Mate sorry for the trouble, ignore the previous 2 messages. the code is working fine. There was space between "Worksheets(" Activity Data")".
However, there is still on small issue.
The code is not re arranging the rows on the master file. By rearranging i mean, its not copying the Last used row at the top and the second last used row below it.
Its just pasting it in the same order as it is there in the workbooks.

when data is getting pasted on the master file, instead of the copying the Row 1 first and Row 2 after that, I want Row 2 to be pasted first and after that Row 1 right below it.
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Good to hear that, it's okay for you. Can you share the workbook, you can remove any confidential information, if there? Upload the file in gdrive & share the link. It should paste the last row at first & then the second last.
 
Upvote 0
Do you want me to upload the workbooks from which i'm copying the data or the workbook on which I'm using the VBA code?
and how can i upload the workbook here? Sorry, Never done that before
 
Upvote 0
upload the file from which you are copying the data, upload in google drive the share the link.
use below yellow highlited to share the link
1601187582582.png
 
Upvote 0
Hello there
Below is the link of the workbooks which you asked for:

Excel Files

Just wanted to ask, while running the code I noticed one thing, that there is no code to unprotect the worksheet ("Activity Data"), there is one to open these workbooks.
Is it possible that this is happening because of this reason?
 
Upvote 0
Hello mehidy1437. sorry to trouble you again after a long time for the same thing.
Just wanted to ask, did you get any solution for the above mentioned query??
 
Upvote 0

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

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