VBA Macro To Loop Through Folders And Select Files With Certain Criteria

Feb 6, 2014

I'm looking to create a macro that does the following:

- Loops through a folder structure
- Opens a specific subfolder within each folder based on name (i.e. "*Financials*")
- Opens a specific file within that subfolder "*Financials*" based on both most recently modified a naming criteria (i.e. "*Model*")
- Performs an action on that file (a macro created elsewhere that I assume I can call here)
- Closes that file, and moves on to the next file matching the criteria above

As a self-diagnosed VBA novice, I'm having trouble adapting code found in various forums for my specific project.

I've found examples that address aspects of my desired macro or do similar things, but I can't figure out how to put them together.

Open most recent file:

[Code] .....

Loop through folders and subfolders and print all files:

[Code] ....

View 1 Replies


ADVERTISEMENT

Loop Through Files In Dated Folders

Nov 30, 2008

each folder is named by date, for example a folder name may be 04-01-2008. Within each folder there is one xls file named daily.xls.
(this never changes)
In date order from the folder name, I need to open each daily.xls run my code, then close daily.xls and then open the next one etc until there are no more.

example path is C:4-01-2008daily.xls

View 9 Replies View Related

Macro To Select Multiple Files And Copy Them Based On Criteria?

Mar 10, 2013

This is what I am trying to achieve:

1) Prompt user to select a file (or multiple files)

2) Copy the files that meet certain criteria to a folder

My attempt (fail):
VB:

Sub FILES2SFTP() Dim FileNames As Variant Dim I As Integer Dim fso As Variant Dim Data As String ChDrive "G:" ChDir "G:TEST" Data = InputBox("Enter the date", "Enter the date", Format(Application.WorksheetFunction.WorkDay(Date, -1), "yyyymmdd")) Set fso = [code]....
I get error 424 object not found in this line:

If fso.getfilename(FileNames(I).Name) = ("Name1" & Data & ".xls" Or "Name2" & Data & ".xls") Then

View 3 Replies View Related

Use Excel VBA To Copy Multiple Files From Different Source Folders To Different Folders?

Jun 12, 2014

I need to back up files, which may be excel or MSword, by copying them from different directories, say C: to the backup directory, say X:. any vba to backup the files? I expect the vba can copy all file listed in column A.

E.g. C: est1File1.xls or C: est2File2.doc, and then pasted to the directory in column B. e.g. X:BackupFolder1 or X:BackupFolder2.

View 1 Replies View Related

Consolidate Data From Multiple Files Within Folders & Sub Folders

Aug 16, 2009

I have managed to get this far - I have put together a macro (from different threads on this site) that opens closed workbooks, copies data in one of the sheets (same sheet in each of 28 books), and pastes the data it into a master book sheet, each paste starting below the last. So that bit is working. The first bit of help I need is a line of code that will make the macro loop through a number of sub folders in a main folder. My code at the moment works as long as I specify a path that ends with the name of one subfolder, and it only loops through this subfolder. I would like the path to end at the folder that holds all the subfolders ('Workbooks' in the path below), and then add some code that tells it to apply the macro to all subfolders in this folder, so it loops through them all.

The second issue is that after the macro goes to the closed book(s), copies the data in there and pastes it into the master sheet (into columns E:FG), I then need it to go back to the workbook it just copied from, go the same sheet, to three specific cells on that sheet (FH1:FH3), copy the content, go back to the master sheet, and now repeatedly paste the content (values only and transposed) of these three cells into three cells (in columns B:D, with row number being dependent on what rows the first lot of data was copied into) next to every row it just previously pasted in for me. When it loops to the next workbook, it needs to do the same, and the three cells will have different content than the ones in the previous workbook paste.

I dont know how to define the range it needs to paste into the second time. I tried using the definition I used for the first paste (MCDrow), to tell it that it is the same rows, just different columns, but this is not working.

Here is what I have so far, which does the first part of what I need, except for needing a way to have it loop through all subfolder in the 'Workbooks' folder (at the moment it lists Barwon South West as a subfolder in that path, but I actually have multiple subfolders, not all called Barwon South (all different names) that it needs to loop through and do both the first and the second paste for. I have taken out the code I was trying to use to do the second paste, as this was not working and the code is pretty messy as it is (I sort of bumble along, being so new, and I know the code is not very clean or efficient!).

Can someone help me put in the few lines I need to loop through all my subfolders (if you give me an example I can probably extrapolate), but to get you started, three of the subfolders are Barwon South West, Eastern Region and Gippsland. And can someone help me put in the code that will do the second paste for each workbook?

Sub Click2()

Application. ScreenUpdating = True
Dim MCDrow As Long
'Dim SubFolders As String
MCDrow = ThisWorkbook.Sheets("Client Data").Range("A65536").End(xlUp).Row

Fpath = "Q:Clinical ServicesCS Statewide DatabaseWorkbooksBarwon South West" ' change to your directory
'SubFolders = True
Fname = Dir(Fpath & "*.xls")
Do While Fname <> ""

ThisWorkbook.Sheets("Client Data").Unprotect

Workbooks.Open Fpath & Fname
Worksheets("Client Data").Activate
Worksheets("Client Data").Unprotect

View 6 Replies View Related

Using Macro To Print Various Word And Excel Files In Different Folders

Jun 2, 2014

What I want to create is an excel file such as:

A
B
C

1
File
Qty
Yes/No

[Code]......

Where Qty sets how many copies for that particular doc, and Yes/No determines if it needs to be printed at all.

Each file will be in a subfolder relative to the excel file.

So it would work like this (somehow)

If Cell, C2 = 1, then print CurrDirSection1examplefile.xlsx, qty= B2 , defaultprinter
If Cell, C3 = 1, then print CurrDirSection2examplefile.docx, qty= B3 , defaultprinter
If Cell, C4 = 1, then print CurrDirSection2examplefile.docx, qty= B4 , defaultprinter

and so forth.

View 3 Replies View Related

Create Macro That Will List All Folders / Subfolders And Files In Order In Excel?

Feb 8, 2013

My problem is to create a macro that will list all folders, subfolders and files in an order in excel. So that I know which folders and files belong to which main folder, more like a hierarchy.

For example,
Sourcefolder
Subfolder 1
File1

I have code that list all files in folders and subfolders, but it does not put them in a hierarchy fashion.

View 8 Replies View Related

Loop Through Folders And Create Folders?

Mar 31, 2014

I want to open C:Projects

and for each Project in Projects, create a new folder with the actual month (e.g. "03" or "12")

Im running the Makro from a file where I'd like to put the month in a cell -> so the code refers to cell("A1") wich has value 01 and creates a folder "01" in every projectfolder in C:Projects

View 3 Replies View Related

Copy And Rename Files In VBA By Searching Multiple Folders For Files?

Jan 4, 2014

how I can loop through folders to select files starting with a certain word and copy all of them to a different folder and rename them. The folder structure is given below

Company 1(parent folder)
North South East(sub folder) West(sub folder)
Jan Feb Mar.... Dec Jan Feb Mar.... Dec Jan Feb Mar.... Dec Jan Feb Mar.... Dec

In the above structure, the files are present inside each folders Jan, feb...Dec under the regions North, South East n west. note that I have to select files starting with "Sales" and copy them into a new folder(say results) and rename copied files as Sales1.xls, Sales2.xls etc. (Files are not present in the folder company1, north, south, east and east.)

View 1 Replies View Related

Making Macro That Will Create New Folders And Then Save In Those Folders?

May 21, 2014

I am attempting to create a macro that will create a new folder on the network drive Arie:quotes that will create the new folder and save the the file in the new folder. I need the name of the file and folder to be set to the master (worksheet) cell "B3". and then if possible have it be conditional that if cell "B40" on the same master (worksheet) has a value to have the value added at the end of the filename via hyphen. This workbook has numerous worksheets in it approx 15 worksheets. I am not sure if this is relevant.

As an brief example.

Cell "B3" on the master(worksheet) contains a job name I-40 bridge
So initially I would like the Folder name and file name to be I-40 Bridge

However once the Cell "B40" has been filled in with the value 54213 then I would like the file to renamed to I-40 Bridge-54213 and saved within the same folder.

I am currently working with Excel 2007 Pro edition on windows 8. I

I have found several other macro's that work with an auto save but i am unsure of how to change the directory to the network drive and show the correct file paths. This is the example of the macro I am currently working with.

Public Sub SaveAsA1()
ThisFile = Range("B3").ValueMkDir "C:NewFolder"ChDir "C:NewFolder"ActiveWorkbook.SaveAs Filename:=ThisFileEnd Sub

View 4 Replies View Related

Loop Through Files And A Run A Macro

Aug 4, 2009

I want to loop through all files in a directory and run a macro named "Main" in each file. The file that contain the loop-macro will stay in the same directory as the files I loop through.

View 4 Replies View Related

Files Within Sub Folders

May 29, 2007

I am using the code below to list all files within a specific folder however I need to know about ALL the files within the underlying sub folders (of which there are many).

I have tried wildcards when declaring the folder but it does not work.

Sub List_Files()

Dim fsoObj As Object 'Scripting.FileSystemObject
Dim fsoMapp As Object 'Scripting.Folder
Dim fsoFil As Object 'Scripting.File
Dim sFolder As String
Dim i As Long
Dim filtype As String
sFolder = "V:T-ASSETAsset ScotlandNetwork Capacity Section1 Business Plan"

Set fsoObj = CreateObject("Scripting.FileSystemObject")

'Set fsoObj = New Scripting.FileSystemObject........................

View 9 Replies View Related

Macro Run As A Loop On All Files In A Given Folder

Mar 22, 2008

macro to run as a loop on all files in a given folder but allow for the option to Browse for the folder I want this to run in?

Sub CleanUp()
Dim i#, rng As Range
With Application
.ScreenUpdating = False
.DisplayAlerts = False
For i = Worksheets.Count To 1 Step -1
If Worksheets.Count = 1 Then GoTo e
Set rng = Intersect(Worksheets(i).[11:11,13:13,23:23,25:25], _
Worksheets(i).UsedRange)
With rng
.Replace What:=Chr(32), Replacement:="", LookAt:=xlPart
.Replace What:=Chr(160), Replacement:="", LookAt:=xlPart
End With...........................

View 10 Replies View Related

Loop Through Folder, Run Macro On All Files

Mar 5, 2009

I am trying to process 60+ data files. I've recorded/written macros that do all of the processing, and now I would like to write a loop that will go through a folder with the data files (.txt, tab delimitted), and run the code on each file. I am using the Mac Version of Excel 2004. I have tried to adapt some code that I found in the forums, but after fixing a few errors, now nothing is happening when I run the code. The code is below. I have it in a module in the VBA editor.

length of the code; I'm assuming the problem is in the very beginning or end (the code I added for the loop), as the code in the middle that does the actual processing works.

Sub ProcessData()

Dim strDocPath As String
Dim strCurrentFile As String

strDocPath = "Macintosh HD"":Users:thomasarmstrong:Desktop:SF08_Macrosv2:Loop_test:directory:"
strCurrentFile = Dir(strDocPath & MacID("TEXT"))

View 9 Replies View Related

VBA Saving PDF Files Into Folders

Sep 29, 2013

I have this code that generates excel pages as pdf files, but I can't get it to save it in its individual folders. For example, I have the sheet name as Brian Lin, I want to save the pdf into the folder called Brian Lin, then if I have the sheet name as Lilly Tran, the pdf should save into the folder called Lilly Tran. Here's my code. The code is one step short of what I want it to do, it saves in the folder right before the name folders.

Code:

Sub Macro1()
Dim wsh As Worksheet, vWshs
Dim sFolderName As String, set_Path As String, name As String, hwas As String, sname As String, swsname As String
vWshs = Array("Rates", "Inputs")
set_Path = "C:UsersJonathanDesktopFaFundInvoices" & hwas & ""

[Code]...

View 4 Replies View Related

Finding Files In Folders

Jun 12, 2008

I need to design code that can check to make sure a specific file is found before it executes.

Our files are all going to start with the same words followed by the date

ex. Name 10-2-1998

I want the code to be dynamic and be able to check in the folder that the current excel sheet is in for the file i need and then have it ready for use in my macro.

View 9 Replies View Related

List Files And Folders

Jun 19, 2008

I have a folder that has a bunch of other folders in it, each containing many files. Is there an easy way to get a file directory into excel, so that I now would have a spreadsheet that tells me what all the folders are and what files are in each one?

short example: main folder = mystuff
subfolder1 = my pix, contains pic1 and pic2
subfolder2 = otherstuff, contains otherstuff1 and otherstuff2

Can excel find these things and give me a list indicating that the 'mystuff' folder contains these two folders, and also list the two files under each folder?

View 6 Replies View Related

How To Assign Macros On All Files In Sub Folders

Apr 13, 2014

I have my main directory C:Perinatal Within that directory I have folders that contain results for each medical conditions (see attachments)

Within each subdirectory are 6 workbooks with (untidy) data results, all the same format just different numbers for each analysis. I want my MAIN_MACRO to loop through every single .xls file in the C:Perinatal, cleaning up the data of all files, and then saving.

I can use the following code to cycle through each folder one at a time, however, I'd like to loop through every subdirectory of

C:Perinatal instead to speed up the process, rather than having to change the path for each subfolder (e.g. C:PerinatalCANCERALL). Is this possible?

If this isn't possible how can I create a 'MyPaths' list and loop through all those directories.

[Code] ......

Attached Images :

folders.jpg‎
CANCERJPG.jpg‎

View 1 Replies View Related

Moving Files From Folder And Sub-folders

Jan 29, 2014

I need to move all the "Excel" files which are available in Folder and also the Subfolders from this location.

I need all the Excel files.

I found a code which is able to move only from Folder but not from the Subfolder.

HTML Code: 

Sub Move_Certain_Files_To_New_Folder()
'This example move all Excel files from FromPath to ToPath.
'Note: It will create the folder ToPath for you with a date-time stamp
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
Dim FNames As String

[code].....

View 3 Replies View Related

Create Folders & Copy Files To It

Jun 26, 2007

I need to create a macro that will allow the user to create a directory based on a template directory, with all directory paths/ names stored in workbook cells:

1) Create a new directory (name/path specified in workbook)
2) Go to a template directory & copy the entire directory, subdirectories & all files
3) Paste them into the new directory
4) Save a copy of the workbook into the specified location from (3)

Example:
X:Templates
-copy all to-
X:Projects(Value specified in workbook cell)

View 3 Replies View Related

Breakdown Of All Folders & Files In Directory

Sep 12, 2007

I am trying to specify all files and folders in a directory and the only help I found was the following thread: Create Index Of Files In Folder That works really well but, what I need is to specify all the folders and all the files under a directory, does any of you awsome hacks have an idea of how I can enhance the macro if the thread posted above?

View 3 Replies View Related

Hyperlink To Files Including Sub Folders

Aug 5, 2008

I am currently using the following code found in this post: Create Index Of Files In Folder

I am not familiar with VB at all. All I want to do is be able to index the sub-directories contained within the main directory. Is there a simple way to edit this code? ...

View 9 Replies View Related

Select Folders In Excel Vba

Jun 26, 2014

I want to open 2 or more folders in excel vba how its possible.

View 4 Replies View Related

Loop Macro To Open/copy List Of Files

Oct 24, 2007

I am trying to write a macro to open a specific file (i.e site 1) and copy a range and paste that data into another workbook (Comparisons).

I can manage to write the macro to do this one by one, but ideally I would like to write some sort of loop macro that will go down a specific range of files (column A), listing the copy range (column B), and where to paste (column C) until it hits a blank row.

That way I do not have to keep re-programming my comparison spreadsheet every time a new site opens.

View 9 Replies View Related

Renaming Files In Infinite Number Of Sub Folders?

Feb 25, 2014

I have written a procedure that renames excel files within a folder based on certain words being in the title. It works for the files in the first lot of subfolders but I am trying to work out how to get it to rename all files in all subfolders regardless of the level.

I've seen similar things done using recursive subs but I have been trying to convert some of the sample codes I've found online to fit my situation and am not having much luck.

how to manipulate my code into something like a recursive procedure or anything else that will do what I want.

Here is my code.

[Code] .....

View 2 Replies View Related

How To Traverse Folders And Files On SharePoint With Excel VBA

Mar 9, 2011

how to traverse all files in SharePoint folders using Excel VBA and have not come across an answer I could use because, by configuration design, I cannot map a SharePoint URL to a drive letter.

Here it is:

Sub SPDir()
Dim wb As Workbook
Dim dummyFile As String
'
' The file specified by dummyFile must reside in SharePoint in order to use SharedWorkspace
' The way the code is set up, the path and filename in dummyFile should NOT substitute %20 instead of spaces
' There is logic later to ignore dummyFile on output
' Substitute your own dummy file name below

[code].....

View 9 Replies View Related

Write Script Which Returns All Files And Folders?

Nov 2, 2012

I need to write a script which returns all files and folders. The script should also work on a Mac pcs. The "Windows" script works without any problems but I have real problems with the Mac script:

Code:
Public Sub List_Files_Mac()
Dim sDir As String
Dim sFile As String

[Code]...

This script doesn't return the folders and long filenames are truncated.

View 4 Replies View Related

Search Folders And Create Hyperlinks To Files

Feb 18, 2009

I have a list of file names sans extension in column A. I want to search a folder I specify and if file is found create a hyperlink to said file either in a new cell or in column A.

The code I have does the search fine, but its hyperlinking is offset and I can’t get it to match the link to the file name.

Also it breaks if it can not find the file.

Here is the code...

View 9 Replies View Related

Loop Through All Folders In A Directory - Find Matching File

Jun 28, 2014

Code that loops through all folders in a directory, to check each workbook in each of those folders. If any workbook name in any of these folders matches a predefined string, then open this workbook for some other codes to run.

View 2 Replies View Related

Looping Through Files In Multiple Folders Searching For Certain Values

May 1, 2013

I'm trying to code a macro to search through all the files in certain folders to find a value defined by the user. The rows containing that value will then be copied and pasted into a separate workbook. My boss currently planning on storing about 550 different files (90 days worth of data) between these folders, but there is the possibility of years worth of data collection, should he change his mind about the 90 days, so I'd like the code to be efficient if possible. What I've tried to do is search each file for the value, then set a range equal the first row containing that value. If the value is found in that workbook, the code loops through the worksheet, adding all the other rows with that value to the range. Then the file looping exits (the values I'm looking for are only contained in one of the workbooks) and the range is copied and pasted into the master workbook. Each workbook has only one sheet.

I'm currently getting a run-time error "13" Type Mismatch error when it gets to the line where the code is supposed to find the value and initialize the range.

Code:
Private Sub SubmitButton_Click()

Dim mybook As Workbook
Dim masterbook As Workbook
Dim rownunm As Long
Dim pathparts(1 To 5) As String

[Code] ......

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved