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.
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:
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.
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 <> ""
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
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.)
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........................
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 & ""
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.
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?
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.
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
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)
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?
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? ...
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.
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
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.
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.
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.
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
I am not so sure this can be done through a macro, but here is what I want.
I have a folder with say around 1000 excel workbooks. I need a macro to
1. Open each file in the directory specified 2. Read the contents of a particular cell, for ex, A1 3. Create a folder in the same directory with the value of A1 4. And, move the excel workbook itself to that folder.
5. Then it opens the next file 6. If the value of A1 is the same as that in the first workbook, it simply moves it to the already created folder, else it creates another folder with the new value of A1 and moves it there.
Hope I am not too confusing. Searching the forums does not give me an exact picture of how it can be done.
Note: I would also like an option where when I execute the macro, it should pop up with an option to select the folder in which the files are, since the files are not always saved in a static file path.
I am trying to open excel files in a file path which includes folders which also have excel folders i wish to open there are quite a few.
At the moment i am working with this code but it fails to open excel files which are within the folders in the specified file path. Its fine for excel workbooks in the folder specified by file path.
This is the code
Sub RunCodeOnAllXLSFiles() Dim lCount As Long Dim wbResults As Workbook
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.
For the last year I have been downloading cash register X1 and Z1 files onto an SD card. Each night's files (X1 & Z1) are stored in a new folder with the naming convention "RP + [DATE]", (EX: RP120910, [YYMMDD]). I have 265 folders! I need to go through each folder, select the Z1 file (I don't care about the X1, X2 or Z2 files) and rename each with the following naming convention: "Z1 + folder_name + .xls". For Example: Folder RP120910 contains the Z1 file named "Z1_T1729.ECR". After renaming, the file should be "Z1RP120910.xls". As the VBA code loops through each subfolder, selecting and renaming the Z1 files, I would like the new files to be placed into a new folder named "Z1Files". Note: All subfolders are currently in a directory named "RegisterFiles". I am using Excel 2010.
I'm trying to make a macro that will search a folder and move all files from 2008 into another folder. So far it can do this, but what i need is for it to search subfolders also. At this point it moves all files from the main folder, but nothing from the subfolders.
Option Explicit ' Reference site [url]
Sub Copy_and_Rename_To_New_Folder()
Dim objFSO As FileSystemObject Dim objFolder As Folder Dim PathExists As Boolean Dim objFile As File Dim SrcFolder As String Dim DestFolder As String Dim x, Counter As Integer, Overwrite As String, strNewFileName As String Dim strName As String, strMid As String, strExt As String Dim LastModYear
Macro which loops through a number of files and calls the same macro in each of them. Unfortunately when I add "Application.Run..." to the code, it no longer loops through the process and instead stops after updating the first file in the loop. If I remove the "Application.Run..." code and add any other code, the loop works fine and it continues through the process repeating all the steps for each file found.
Why it stops after one file when using "Application.Run..." to call the macros?
NB I have a list of path and file names starting in row 8 of columns A and C. Each file in the list has a macro called UpdateS1 and promoupdate1.
Sub C_Run_Loop_Macro() Dim lastRow As Long Dim i As Long