The macro reads a csv file and delete all rows containing "2023".
My files have 200 000 plus rows.
Can anyone suggest a way to make it faster.
Any help will be appreciated.
My files have 200 000 plus rows.
Can anyone suggest a way to make it faster.
Any help will be appreciated.
VBA Code:
Sub DeleteRowsContaining2023()
Dim filePath As String
Dim wb As Workbook
Dim ws As Worksheet
Dim csvData As String
Dim lines() As String
Dim filteredLines As String
Dim i As Long
Dim j As Long
' Prompt user to select the CSV file
filePath = Application.GetOpenFilename("CSV Files (*.csv), *.csv")
If filePath = "False" Then
MsgBox "No file selected. Exiting macro."
Exit Sub
End If
' Read CSV file content into a string
Open filePath For Input As #1
csvData = Input$(LOF(1), 1)
Close #1
' Split the CSV data into lines
lines = Split(csvData, vbCrLf)
' Loop through each line and filter out lines containing "2023"
For i = LBound(lines) To UBound(lines)
If InStr(1, lines(i), "2023") = 0 Then
filteredLines = filteredLines & lines(i) & vbCrLf
End If
Next i
' Write the filtered data back to the CSV file
Open filePath For Output As #1
Print #1, Left(filteredLines, Len(filteredLines) - 1)
Close #1
MsgBox "Rows containing '2023' have been deleted from the CSV file.", vbInformation
End Sub