I have written the below code in order to sum a specified column in a closed workbook.
The summary sheet I have has the file location/name in Column B, Sum Column Location in Column C, Conversion of Column Letter to Number in Column D, and then the Sum Amount in Column E.
I currently have about 50 workbooks that I need to pull the data from so I created a loop that first tests to see if a file exists (the file names change daily and are available at different times each day), if the file exists then it opens the workbook and sums the specified column for that workbook, puts that Sum in the summary sheet Column E and then closes the book and then moves to the next Row. It takes a while to run and since a lot of you are way better at coding than I am I was wondering if/how I could make this run more optimally. Any help is greatly appreciated.
Here is my current code:
Sub GetClosedPNL2()
Application.ScreenUpdating = False
Dim wbBook1 As Workbook: Set wbBook1 = ThisWorkbook
Dim src As Workbook
Dim lCol As Integer
Dim LastRow As Long
Dim DataRange As Range
Dim Cll As Range
Dim strFileName As String
Dim strFileExists As String
LastRow = Sheets("AccountMap").Cells(Sheets("AccountMap").Rows.Count, "B").End(xlUp).Row
Set DataRange = Sheets("AccountMap").Range("B2:B" & LastRow)
For Each Cll In DataRange
strFileName = (Cll.Value)
strFileExists = Dir(strFileName)
If strFileExists = "" Then
GoTo Line2
Else
GoTo Line1
Line1:
Set src = Workbooks.Open(Cll.Value, ReadOnly:=True)
lCol = Cll.Offset(0, 2).Value
Cll.Offset(0, 3) = Application.Sum(src.Sheets(1).Columns(lCol))
src.Close False
Set src = Nothing
Line2:
Next Cll
End Sub