Transfer Data for Multiple into One Master Sheet

sg2209

Board Regular
Joined
Oct 27, 2017
Messages
117
Office Version
  1. 2016
hi ,


i have written the below code by reading some threads on Mr Excel .

I am trying to transfer data from Multiple workbooks into a master excel workbook automatically using VBA.

i have saved my excel sheets (.xlsb) in a folder and also a Master Sheet with name of Collated C:\Users\HP\Desktop\UPD

All the sheets are saved by name of upd05122017, upd06122017. i have also input a code in This Workbook so when i opened the master file , all the data is being captured .

below is the code , please help

Sub LoopThroughDirectory()
Dim MyFile As String
Dim erow
MyFile = Dir("C:\Users\HP\Desktop\UPD")


Do While Len(MyFile) > 0
If MyFile = "Collated.xlsm" Then
Exit Sub
End If


Workbooks.Open (MyFile)
Range("A2:S5000").Copy
ActiveWorkbook.Close


erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Raw").Range(Cells(erow, 1), Cells(erow, 19))


MyFile = Dir
Loop


End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Untested, but try
Code:
Sub LoopThroughDirectory()
   Dim MyFile As String
   Dim MyPath As String
   Dim Wbk As Workbook
   Dim DestSht As Worksheet
   
   Set DestSht = ThisWorkbook.Sheets("Raw")
   MyPath = "C:\Users\HP\Desktop\UPD\"
   MyFile = Dir(MyPath)
   
   Do While Len(MyFile) > 0
      If MyFile <> "Collated.xlsm" Then
      
         Set Wbk = Workbooks.Open(MyPath & MyFile)
         Wbk.Sheets("[COLOR=#ff0000]Sheet1[/COLOR]").Range("A2:S5000").copy DestSht.Range("A" & Rows.Count).End(xlUp).Offset(1)
   
         Wbk.Close False
      End If
      
      MyFile = Dir
   Loop

End Sub
Change the sheet name in red to match the sheet name you are trying to copy from
 
Upvote 0
thank you it works , however when i saved the Collated Workbook and again open it , it once again copy the data so Duplicate data is there , how can i go for it so it does not copy the data from the sheet that was already copied.

if data was copied from upd05122017 , it should not again copy from that sheet .
 
Upvote 0
The easiest option is to clear the collated sheet first.
Code:
Sub LoopThroughDirectory()
   Dim MyFile As String
   Dim MyPath As String
   Dim Wbk As Workbook
   Dim DestSht As Worksheet
   
   Set DestSht = ThisWorkbook.Sheets("Raw")
   DestSht.UsedRange.Offset(1).Clear
   MyPath = "C:\Users\HP\Desktop\UPD\"
   MyFile = Dir(MyPath)
   
   Do While Len(MyFile) > 0
      If MyFile <> "Collated.xlsm" Then
      
         Set Wbk = Workbooks.Open(MyPath & MyFile)
         Wbk.Sheets("Sheet1").Range("A2:S5000").copy DestSht.Range("A" & Rows.Count).End(xlUp).Offset(1)
   
         Wbk.Close False
      End If
      
      MyFile = Dir
   Loop

End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
@Fluff , its not working when i am using the path "\\124.4.4.50\9100201_n01\DASHBOARD&REPORTING\2017\December\UPD"
 
Upvote 0
AFAIK, you cannot use an IP address with Dir. It needs to be a mapped drive.
 
Upvote 0
i did try by mapping it , and the drive is Z Should i use like this

"X:\DASHBOARD&REPORTING\2017\December\UPD"
 
Upvote 0
That would depend on how you've mapped it, but if you mapped it to the Z drive, the path name should begin with Z not X.
Best bet is try it & see.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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