Results 1 to 3 of 3
  1. #1
    2 Star Lounger
    Join Date
    Feb 2010
    Location
    Denver, CO
    Posts
    191
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  2. #2
    5 Star Lounger AndrewKKWalker's Avatar
    Join Date
    Apr 2001
    Location
    Cambridge, UK
    Posts
    1,020
    Thanks
    0
    Thanked 3 Times in 3 Posts
    A bit more info would be useful.
    Which columns, data types, All CSV Files in Folder, What to do with them after import.
    Andrew

  3. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    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!

    My Systems: Desktop Specs
    Laptop Specs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •