Sub import_csv_and_filter()
Dim file As FileDialog
Dim filePath As String
Dim csv As String
Dim wsheet As String
Application.DisplayAlerts = False
Application.StatusBar = True
wsheet = ActiveWorkbook.Name
Set file = Application.FileDialog(msoFileDialogFolderPicker)
file.Title = "Folder Selection:"
If file.Show = -1 Then
filePath = file.SelectedItems(1)
Else
Exit Sub
End If
If Right(filePath, 1) <> "\" Then filePath = filePath + "\"
csv = Dir(filePath & "*.csv")
Do While csv <> ""
Application.StatusBar = "Converting: " & csv
Workbooks.Open Filename:=filePath & csv
ImportExpenses ActiveSheet
ActiveWorkbook.Close
Windows(wsheet).Activate
csv = Dir
Loop
Application.StatusBar = False
Application.DisplayAlerts = True
End Sub
Sub ImportExpenses(ws As Worksheet)
Dim lastRow As Long
Dim keyword As String
Dim categoryColumn As Long
Dim descriptionColumn As Long
Dim amountColumn As Long
Dim dateColumn As Long
' Define the keywords and corresponding columns
keyword = "Food" ' Change this keyword as needed
dateColumn = 1 ' Date column
descriptionColumn = 2 ' Description column
amountColumn = 3 ' Amount column
categoryColumn = 4 ' Category column
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1
For Each cell In ws.Range("B1:B" & ws.Cells(ws.Rows.Count, "B").End(xlUp).Row)
If InStr(1, cell.Value, keyword, vbTextCompare) > 0 Then
ws.Cells(lastRow, dateColumn).Value = cell.Offset(0, -1).Value
ws.Cells(lastRow, descriptionColumn).Value = cell.Value
ws.Cells(lastRow, amountColumn).Value = cell.Offset(0, 1).Value
ws.Cells(lastRow, categoryColumn).Value = cell.Offset(0, 2).Value
lastRow = lastRow + 1
End If
Next cell
End Sub