View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
marcus[_3_] marcus[_3_] is offline
external usenet poster
 
Posts: 140
Default Total or summing in a workbook within a folder

Hi Cam

OK so here we go. This opens all the files in a specific directory
and places “Total” at the bottom of column A, sums the numbers in
Columns D and E placing the formula in the same row as column A. It
assumes the Numbers start in Row 2 so you may have to change this to
suit if necessary.

You will need to change the file path “.Lookin = “C:\Yourpathhere
No need to prefix your path with .xls as this is catered for.

It saves the changes at present but you might like to run a trial with
a sample of files which open make the changes and do not save.

wkbk.Close SaveChanges:=False

Good luck with it.

Take care

Marcus


Sub OpenFiles()

Dim wkbk As Workbook
Dim Lw As Integer, Sr As Integer

With Application.FileSearch
.NewSearch
.LookIn = "c:\users\smallman\excel"
.SearchSubFolders = False
.Filename = ".xls"
.FileType = msoFileTypeExcelWorkbooks
If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
Set wkbk = Workbooks.Open(.FoundFiles(i))
Lw = Range("A" & Rows.Count).End(xlUp).Row + 1
Sr = Lw - 1 'for the Sum row
Range("A" & Lw).Value = "Total"
Range("D" & Lw).Value = "=SUM(D2:D" & Sr & ")"
Range("E" & Lw).Value = "=SUM(E2:E" & Sr & ")"

wkbk.Close SaveChanges:=True
Next i

End If
End With
End Sub