-1

I want to delete the record from CSV after reading that row. Please guide me how can I achieve this.

I have found this piece of code :

Sub ImportCSVFile(ByVal filePath As String, ByVal ImportToRow As Integer, ByVal StartColumn As Integer)

Dim line As String
Dim arrayOfElements
Dim element As Variant


Open filePath For Input As #1 ' Open file for input
    Do While Not EOF(1) ' Loop until end of file
        Line Input #1, line
        Debug.Print line
        arrayOfElements = Split(line, ";") 'Split the line into the array.
    Loop
Close #1 ' Close file.
End Sub

It is reading the CSV file and working as required. Can someone please advise that how can I delete the row in CSV file after reading it ?

  • 1
    Welcome to Stack Overflow. People who help out here are not here to just write all the code for you, they are here to help you sort out specific problems you run into when you write your own code. If you are looking for a place to start, I would start with learning how to read a text file. https://stackoverflow.com/questions/20390397/reading-entire-text-file-using-vba – braX Jun 10 '20 at 00:44
  • Thanks for your comment, I have updated my question with piece of code that works fine in reading CSV file. Can you please guide how can I delete the row which I have read all already ? – percydeveloper Jun 10 '20 at 00:51
  • Delete it from the text/csv file? Next, you will next need to learn how to write a text file. You read one file, process it how you want, and write the results to a new file. You dont "delete stuff from the file you are reading" – braX Jun 10 '20 at 00:52
  • Actually the CSV file is log file and I am reading that and sending that data to DB now I want to delete records from log file as I have sent that to DB. What should be the best method for this task ? – percydeveloper Jun 10 '20 at 00:55
  • As I said, people here are not going to write all the code for you. You need to break it down into pieces and learn how to do each piece, and then put those pieces together. Notice now your question has completely changed, as now it involves a database too. This is the reason why no one is going to just try to write it all for you. – braX Jun 10 '20 at 00:56
  • Alrighty then. Good luck. :) – braX Jun 10 '20 at 01:00

1 Answers1

0

If I were to be doing this I would use a FileSystemObject to read a line rather than opening the file directly. I am not sure what it is you want to do for editing the log file itself but I you want to loop through each line and then through each token the code I would use is:

Sub ImportCSVFile(ByVal filePath As String, Optional ByVal ImportToRow As Long = 0, Optional ByVal StartColumn As Long = 1)

'Declare variables.
Dim FSO As Object, sourceFile As Object
Dim line As String, delimiter As String, ForReading As Integer: ForReading = 1
Dim i As Long: i = 1
Dim arrayOfElements As Variant, element As Variant

'Create file system object and open filePath .csv parameter.
Set FSO = CreateObject("Scripting.FileSystemObject")
Set sourceFile = FSO.OpenTextFile(filePath)

delimiter = ";"

'Main loop, reads each line of the .csv file.
Do While Not sourceFile.AtEndOfStream

    'Checks if the line being read is at or after the StartColumn parameter.
    If i >= StartColumn Then

        'Splits line string into an array based on the delimiter.
        line = sourceFile.ReadLine
        arrayOfElements = Split(line, delimiter)

        'Loops through array elements.
        For Each element In arrayOfElements
            'Your code here.
        Next element

    End If

    'If a value for ImportToRow is provided checks if that line has been reached.
    If Not ImportToRow = 0 Then

        If i >= ImportToRow Then
            Exit Do
        End If

    End If

i = i + 1: Loop

'Cleans up objects.
Set sourceFile = Nothing: Set FSO = Nothing

End Sub