locked
Copying specific rows from a set of CSV files RRS feed

  • Question

  • User1749399186 posted

    Hello! I'm trying to make a program in VB to take the first 25 rows of a little over 150 CSV files and copy them into one larger dataset. Each dataset has a top row with notes and a header row that must be skipped, so the data I'd like to copy is on rows 3-28. I'm able to select the files and read them, but I can't figure out how to actually select the rows I want and opy them over. Here's what I have so far:

            For Each file In files
    
                Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(file)
                    MyReader.TextFieldType = FileIO.FieldType.Delimited
                    MyReader.SetDelimiters(",")
    
                    Dim currentRow As String()
    
                    While Not MyReader.EndOfData
                        currentRow = MyReader.ReadFields()
                        Dim currentField As String
                        For Each currentField In currentRow
                            Dim x As Integer = MyReader.LineNumber
                            Select Case x
                                Case x <= 3
                                Case x > 28
                                Case x >= 4
                                    strLine &= currentField & ","
                                    linesToAdd(x - 1) = strLine
                                    MsgBox(linesToAdd(x)) 'checking if it entered right, returns infinite blank message boxes now
                            End Select
                        Next
                    End While
                End Using
            Next

    Sorry if it's sloppy, I haven't coded anything in a while and this has been pretty daunting. Any help is greatly appreciated!

    Monday, January 27, 2020 11:49 PM

All replies

  • User-719153870 posted

    Hi Rekos,

    What's the program type? A Windows Form application?

    Each dataset has a top row with notes and a header row that must be skipped, so the data I'd like to copy is on rows 3-28.

    Do you mean you want to copy 3rd-27th rows(3-28 should be 26 rows) of your CSV file to a dataset?

    In this case, you will need to use the Skip() to the first two rows and then select 25 rows out, you can refer to this answer which is coded with C# but can be converted easily.

    Best Regard,

    Yang Shen

    Wednesday, January 29, 2020 2:16 AM