• VBA solution to extract csv file data

    Author
    Topic
    #466884

    Hey gang–

    Trying to finish a project and know there has to be a VBA solution to this. I have about 120 .csv files in a folder. These files are structured identically, but have different data and different number of lines. I need to extract 3 of the columns and place them in a single worksheet.

    I’m sure the base code for something like this has to already exist.

    Thanks for your help with this.

    Viewing 1 reply thread
    Author
    Replies
    • #1210652

      A bit more info would be useful.
      Which columns, data types, All CSV Files in Folder, What to do with them after import.

    • #1210654

      Here’s a quick layout in pseudo code (especially lines starting w/#).

      Code:
      Private Sub ReadCSV()
      
         Dim zFileList     As String
         Dim zFilePath     As String
         Dim oCurWkBk      As Object
         Dim oNewWkBk      As Object
      
             
         Set oCurWkBk = ActiveWorkbook        '*** Save reference to active workbook
        
         zFileList = Dir("C:path*.csv")     '*** Replace C:path with the location of your .csv files
         
         Do While zFileList  ""             '*** Loop while there are files in the directory
      
           Set oNewWkBk = Workbooks.Open(Filename:=zFileName)  '*** Save reference to .csv file.
      
           #Select the cols/rows to copy
           Selection.copy
           oCurWkBk.Activate                  '*** Switch to base workbook
           #move to desired paste location in sheet of workbook where they are to be combined
           #Paste
           Application.DisplayAlerts = False  '*** Suppress warning messages
           oNewWkBk.Close                     '*** Close .csv file
           Application.DisplayAlerts = True   '*** Re-enable warning messages
          
      
          zFileList = Dir()                   '*** Get next .csv file in same location
      
         Loop 
         
      End Sub

      OK…I did the research in some old code and here’s a more complete template for you to complete. Hope this get’s you started.

      RG

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    Viewing 1 reply thread
    Reply To: Reply #1210652 in VBA solution to extract csv file data

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel