Repetitive Task

Chang Chang

New Member
Joined
Aug 24, 2017
Messages
3
Can someone help me with this repetitive task? I need to pull data from a file located in multiple folders with the same file name, then copy selected data into one file.


Network drive name: \\USA\Sales\Inventory
Folder name: Glass
Sub folder: Sales1, Sales2, Sales3, Sales4, … …
File Name: Inventory.xlsx
Data: Cell B4, B5, D6, E8
Combined file name: All.xlsx




The manual process is:

  1. Open file All.xlsx
  2. Open file Invenotry.xlsx in folder Sales1
  3. Copy cell B4, B5, D6, E8
  4. Paste date to file All.xlsx in cell row 6 column C, D, E, F (increment row for the next file; in this case, 7; keep the same columns)
  5. Close file Inventory.xlsx
  6. Select the next folder (Sales2)
  7. Repeat step 2 to 6
  8. Stop when the last folder (SalesXXX) is completed


Thank you in advance.
 
Last edited by a moderator:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
How about
Code:
Sub getData()
   Dim Wbk As Workbook
   Dim Ws As Worksheet
   Dim fso As Object
   Dim Fldr As Object
   Dim Rng As Range
   Dim i As Long, Rw As Long
   
   Rw = 6
   Set Ws = ThisWorkbook.Sheets("[COLOR=#ff0000]data[/COLOR]")
   Set fso = CreateObject("Scripting.FileSystemObject")
Application.ScreenUpdating = False
   For Each Fldr In fso.GetFolder("[SIZE=2][FONT=verdana][COLOR=#000000]\\USA\Sales\Inventory\Glass[/COLOR][/FONT][/SIZE]\").subfolders
      If Fldr.Name Like "Sales*" Then
         Set Wbk = Workbooks.Open(Fldr.Path & "\Inventory.xlsx")
         For Each Rng In Wbk.Sheets("[COLOR=#ff0000]Sheet1[/COLOR]").Range("B4,B5,D6,E8")
            Ws.Range("C" & Rw).Offset(, i).Value = Rng.Value
            i = i + 1
         Next Rng
         Wbk.Close False
         i = 0: Rw = Rw + 1
      End If
   Next
End Sub
This needs to go in the Combined file, which will need to be saved as an xlsm.
Change sheet names in red to suit
 
Upvote 0
Hi Fluff,
For the "data" in your VBA (red font), do I need to change it to something else?

Thank you in advance.


How about
Code:
Sub getData()
   Dim Wbk As Workbook
   Dim Ws As Worksheet
   Dim fso As Object
   Dim Fldr As Object
   Dim Rng As Range
   Dim i As Long, Rw As Long
   
   Rw = 6
   Set Ws = ThisWorkbook.Sheets("[COLOR=#ff0000]data[/COLOR]")
   Set fso = CreateObject("Scripting.FileSystemObject")
Application.ScreenUpdating = False
   For Each Fldr In fso.GetFolder("[SIZE=2][FONT=verdana][COLOR=#000000]\\USA\Sales\Inventory\Glass[/COLOR][/FONT][/SIZE]\").subfolders
      If Fldr.Name Like "Sales*" Then
         Set Wbk = Workbooks.Open(Fldr.Path & "\Inventory.xlsx")
         For Each Rng In Wbk.Sheets("[COLOR=#ff0000]Sheet1[/COLOR]").Range("B4,B5,D6,E8")
            Ws.Range("C" & Rw).Offset(, i).Value = Rng.Value
            i = i + 1
         Next Rng
         Wbk.Close False
         i = 0: Rw = Rw + 1
      End If
   Next
End Sub
This needs to go in the Combined file, which will need to be saved as an xlsm.
Change sheet names in red to suit
 
Upvote 0
Yes, you need to change those sheet names to what ever your sheets are called
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,731
Members
448,987
Latest member
marion_davis

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