Add contents of A1 in all workbooks within a folder
Hi Bob, also, I tried to execute as you stated earlier, and got a
User-defined type not defined on the line
Dim bInfo As BROWSEINFO in the function..
"Bob Phillips" wrote in message
...
Steph,
This code is all inclusive, that is when you run it it will provide a
dialog box that will allow you to navigate to the folder of your choice.
When chosen, it goes through each workbook, and gets the value from cell
A1.
You will need to add some code to process the sum, in variable nTotal,
else
it will just disappear at the end.
Create a new code module in VBA, and copy this into it. Then just run the
ProcessFile macro.
--
HTH
RP
"Steph" wrote in message
...
Hi Bob. Thanks for the code. But how do I call it, or specify which
folder
I want to sum the contents of cell A1? And do I need both the sub and
the
function?
Sorry, but my ignorance is getting the best of me here! Thanks for your
help!
"Bob Phillips" wrote in message
...
Sub ProcessFiles()
Dim FSO As Object
Dim i As Long
Dim sFolder As String
Dim fldr As Object
Dim Folder As Object
Dim file As Object
Dim Files As Object
Fim nTotals as double
Set FSO = CreateObject("Scripting.FileSystemObject")
sFolder = GetFolder
If sFolder < "" Then
Set Folder = FSO.GetFolder(sFolder)
Set Files = Folder.Files
For Each file In Files
If file.Type = "Microsoft Excel Worksheet" Then
Workbooks.Open Filename:=file.Path
With ActiveWorkbook
nTotal = nTotal + .ACtivesheet.Range("AQ1").Value
.Close savechanges:=False
End With
End If
Next file
End If ' sFolder < ""
End Sub
'-------------------------------------------------------------
Function GetFolder(Optional ByVal Name)
'-------------------------------------------------------------
Dim bInfo As BROWSEINFO
Dim path As String
Dim oDialog As Long
If IsMissing(Name) Then Name = "Select a folder."
bInfo.pidlRoot = 0& 'Root folder = Desktop
bInfo.lpszTitle = Name
bInfo.ulFlags = &H1 'Type of directory to
Return
oDialog = SHBrowseForFolder(bInfo) 'display the dialog
'Parse the result
path = Space$(512)
GetFolder = ""
If SHGetPathFromIDList(ByVal oDialog, ByVal path) Then
GetFolder = Left(path, InStr(path, Chr$(0)) - 1)
End If
End Function
--
HTH
RP
"Steph" wrote in message
...
I have a bunch of workbooks within a folder. I need to add the
contents
of
cell A1 in each of the workbooks within the folder, but the names of
the
files within the folder will vary. Can I write a function that will
add
the
contents of all files cell A1?
Much appreicated.
|