I need to write VBA for a csv that will be regularly updated.Im stuck in a loop of errors and idk what to do.
I need to implement an excel 365 macro that :
Opens a csv file with the link : "C:\Users\Dev-01\Desktop\Automated Reports\Approved to Funded Loans\Docs Out to Funded.csv"
The function will have two parameters the filter and the row number
Then extract two values :
First value is the number of rows based on the filter in column 2 .
Second value is the addition of all values in column 6 of the filtered data .
After extracting the data, put the values in the workbook “ Dash Board “ in column 3 and 5 and the row
These are the corresponding rows numbers after the strings to call the function with:
Condition Review 7
Final Underwriting 8
Pre-Doc 9
Clear To Close 10
Docs Ordered 11
Docs Drawn 12
Docs Out 13
Docs Back 14
Funding Conditions 15
Sub GetTotalsFromCSV()
' File path and column setup (adjust as needed)
Const filePath As String = "C:\Users\Dev-01\Desktop\Automated Reports\Approved to Funded Loans\Docs Out to Funded.csv"
Const filterColumn As Integer = 2
Const totalColumn As Integer = 6
' Array of filter values with corresponding row numbers
Dim filterValuesAndRows As Variant
filterValuesAndRows = Array( _
Array("condition review", 7), _
Array("final underwriting", 8), _
Array("Pre-doc", 9), _
Array("clear to close", 10), _
Array("docs ordered", 11), _
Array("docs drawn", 12), _
Array("docs out", 13), _
Array("Docs back", 14), _
Array("funding conditions", 15) _
)
' Open and read CSV data
Dim data As Variant
data = ReadCSVFile(filePath)
' Iterate through filter values and apply filtering and calculations
Dim i As Long
For i = LBound(filterValuesAndRows) To UBound(filterValuesAndRows)
Dim filterValue As String
filterValue = filterValuesAndRows(i)(0)
Dim targetRow As Long
targetRow = filterValuesAndRows(i)(1)
' Filter data based on current value
Dim filteredData As Variant
filteredData = FilterData(data, filterColumn, filterValue)
' Calculate total for filtered data
Dim filteredTotal As Double
filteredTotal = Application.WorksheetFunction.Sum(filteredData, totalColumn)
' Calculate number of filtered rows
Dim filteredRowCount As Long
filteredRowCount = UBound(filteredData, 1) - LBound(filteredData, 1) + 1
' Write total number of rows and filtered total to separate cells
Cells(targetRow, 3).Value = filteredRowCount ' Number of filtered rows
Cells(targetRow, 5).Value = filteredTotal ' Filtered value total
Next i
End Sub
' Helper function to read CSV data
Function ReadCSVFile(filePath As String) As Variant
Dim fso As Object, file As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile(filePath, ForReading)
ReadCSVFile = Split(file.ReadAll, vbCrLf)
file.Close
End Function
' Helper function to filter data
Function FilterData(data As Variant, filterColumn As Integer, filterValue As String) As Variant
Dim filteredArray() As Variant
Dim i As Long, j As Long, k As Long
ReDim filteredArray(LBound(data) To UBound(data), LBound(data, 2) To UBound(data, 2))
For i = LBound(data) To UBound(data)
If data(i, filterColumn) = filterValue Then
For j = LBound(data, 2) To UBound(data, 2)
filteredArray(k, j) = data(i, j)
Next j
k = k + 1
End If
Next i
ReDim Preserve filteredArray(LBound(filteredArray) To k - 1, LBound(filteredArray, 2) To UBound(filteredArray, 2))
FilterData = filteredArray
End Function
It should be a simple thing to do i tried multiple codes online and help from associates. This is the last thing i ended up with.