|
-
Feb 15th, 2012, 08:52 AM
#1
Thread Starter
New Member
[RESOLVED] Modify files by looping through all folders and subfolders
Hi all,
This is my first time on the forums, even though I've been using code off here for the past few weeks. I'm working with Excel 2007 and VBA 6.5 and only have about 2 weeks experience with VBA under my belt so bear with me here. I've done C++ and Matlab before, but macros are still new.
I've been looking for a way to run a macro sub to loop through all files in a chosen folder and all its subfolders. I've found ways to list off files in folders and subfolders, but I can't seem to make them work for my code. I'm basically looking for the simplest code possible that will do this.
My code below opens a data file and template, transfers the data into the template, then overwrites the data file with the nice data template form. Right now it will only loop through all files in a given folder. Thanks in advance for any help.
Code:
Sub Step_Response_FIXED()
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim strDir As String, strFileName As String
'Select a folder dialog popup sets active directory
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please Select a Folder"
.ButtonName = "Select Folder"
.InitialFileName = "C:\Documents and Settings\ktech\Desktop\"
.AllowMultiSelect = False
.Show
If .SelectedItems.count > 0 Then
strDir = .SelectedItems(1) & "\"
Else
MsgBox "No folder was chosen." & vbLf & vbLf & "Please try again.", vbExclamation, "User Cancelled."
Exit Sub
End If
End With
strFileName = Dir(strDir & "*GFE DV 3.4.9 Step Response & Overshoot ID_*.xls")
Do While strFileName <> ""
'Define Workbooks
Dim wbk1 As Workbook
Dim wbk2 As Workbook
Set wbk1 = Workbooks.Open("C:\Documents and Settings\ktech\Desktop\NH Step Response Overshoot Template.xls")
Set wbk2 = Workbooks.Open(strDir & strFileName)
'Transfer data between files
wbk1.Sheets("DATA").Range("A1:AG29").Value = _
wbk2.Sheets("DATA").Range("A1:AG29").Value
wbk1.Sheets("SUMARIZED DATA").Range("A1:AJ2").Value = _
wbk2.Sheets("SUMARIZED DATA").Range("A1:AJ2").Value
wbk1.Sheets("CSV DATA").Range("A1:H15000").Value = _
wbk2.Sheets("CSV DATA").Range("A1:H15000").Value
wbk1.Sheets("TABLES").Range("A1:C17").Value = _
wbk2.Sheets("TABLES").Range("A1:C17").Value
wbk1.Sheets("CHART NAMES").Range("A1:P2").Value = _
wbk2.Sheets("CHART NAMES").Range("A1:P2").Value
'Closes old data file
Windows(strFileName).Activate
ActiveWorkbook.Close False
'Activates template with newly copied data
Windows("NH Step Response Overshoot Template.xls").Activate
Sheets("Pass-Fail Summary").Select
'Pauses for user input
'MsgBox "Press Enter to continue"
'Change directory and save
ChDir strDir
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs filename:=strFileName, FileFormat:=xlNormal
'Close active windows
Windows(strFileName).Activate
ActiveWorkbook.Close False
strFileName = Dir
Loop
Application.ScreenUpdating = True
Application.EnableEvents = True
MsgBox "YOU ARE FINISHED!!!!"
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|