I'm fairly new to vba for excel and I need a little guidance on a macro that I've pieced together from multiple web sources (pasted below). I've search for a couple of days, and nothing I've found gets me what I want.
The macro loops through files in a specific directory and sums values from one cell on a specific sheet. I'd like to expand the macro to record the file name and the specified sheet's cell value (i.e., B53) for each file that it loops through and begin listing them within in A23 and B23 respectively. Can anyone help with this?
I'm using excel 2003 if this is necessary.
Sub XLS2CSV()
Dim lCount As Long
Dim wbResults As Workbook
Dim wbCodeBook As Workbook
Dim sheeetname As String
Dim boookname As String
Dim location As String
Dim csv As String
Dim vaFileName As Variant
Dim wbkData As Workbook
Dim dMySum As Double
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
' Records the total number of errors in the directory files
On Error Resume Next
Set wbCodeBook = ThisWorkbook
With Application.FileSearch
.NewSearch
.LookIn = "S:\Community Services\SOCS Contracts Administration\PCCO\R3_MH\R3_Conversion_Directory" 'the directory to search in
.SearchSubFolders = False
.FileType = msoFileTypeExcelWorkbooks
If .Execute > 0 Then 'workbooks found
Application.ScreenUpdating = False
For Each vaFileName In .FoundFiles 'loop through each found workbook
Set wbkData = Workbooks.Open(Filename:=vaFileName, UpdateLinks:=0) 'open the workbook
With wbkData
With .Worksheets("Notes").Range("B53")
If IsNumeric(.Value) Then dMySum = dMySum + .Value 'add the desired value
End With
.Close savechanges:=False 'close without saving
End With
Next vaFileName
Application.ScreenUpdating = True
Range("B22") = dMySum
Range("B22").Select
Selection.Font.Bold = True
Range("B22").Font.ColorIndex = 0
Else
MsgBox "There were no Excel files found."
End If
End With
The macro loops through files in a specific directory and sums values from one cell on a specific sheet. I'd like to expand the macro to record the file name and the specified sheet's cell value (i.e., B53) for each file that it loops through and begin listing them within in A23 and B23 respectively. Can anyone help with this?
I'm using excel 2003 if this is necessary.
Sub XLS2CSV()
Dim lCount As Long
Dim wbResults As Workbook
Dim wbCodeBook As Workbook
Dim sheeetname As String
Dim boookname As String
Dim location As String
Dim csv As String
Dim vaFileName As Variant
Dim wbkData As Workbook
Dim dMySum As Double
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
' Records the total number of errors in the directory files
On Error Resume Next
Set wbCodeBook = ThisWorkbook
With Application.FileSearch
.NewSearch
.LookIn = "S:\Community Services\SOCS Contracts Administration\PCCO\R3_MH\R3_Conversion_Directory" 'the directory to search in
.SearchSubFolders = False
.FileType = msoFileTypeExcelWorkbooks
If .Execute > 0 Then 'workbooks found
Application.ScreenUpdating = False
For Each vaFileName In .FoundFiles 'loop through each found workbook
Set wbkData = Workbooks.Open(Filename:=vaFileName, UpdateLinks:=0) 'open the workbook
With wbkData
With .Worksheets("Notes").Range("B53")
If IsNumeric(.Value) Then dMySum = dMySum + .Value 'add the desired value
End With
.Close savechanges:=False 'close without saving
End With
Next vaFileName
Application.ScreenUpdating = True
Range("B22") = dMySum
Range("B22").Select
Selection.Font.Bold = True
Range("B22").Font.ColorIndex = 0
Else
MsgBox "There were no Excel files found."
End If
End With