+ Reply to Thread
Results 1 to 2 of 2

Automating CSV imports to the same spreadsheet.

Hybrid View

  1. #1
    Registered User
    Join Date
    03-24-2016
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    1

    Automating CSV imports to the same spreadsheet.

    Hi there,

    I have a series of CSV files which I'd like to collate into one excel worksheet, but I'm struggling to find a pre-written solution. The CSV files are all 2 columns each, with the same number of rows. The first column of each file is identical.

    I would like to create a worksheet which has the following format:

    - first row contains file name which data came from
    - first column contains the first column of first file
    - second column contains the second column of first file
    - third column contains the second column of second file, etc etc

    I've pulled the following code from the web, but I'm not 100% where to start with editing it as I've not used VBA before, so any help would be much appreciated!

    cheers,

    Jamie

    Option Explicit
    
    #If VBA7 Then
        Private Declare PtrSafe Function SetCurrentDirectoryA Lib _
            "kernel32" (ByVal lpPathName As String) As Long
    #Else
        Private Declare Function SetCurrentDirectoryA Lib _
            "kernel32" (ByVal lpPathName As String) As Long
    #End If
    
    Function ChDirNet(szPath As String) As Boolean
    'based on Rob Bovey's code
        Dim lReturn As Long
        lReturn = SetCurrentDirectoryA(szPath)
        ChDirNet = CBool(lReturn <> 0)
    End Function
    
    Sub Get_CSV_Files()
    'For Excel 2000 and higher
        Dim Fnum As Long
        Dim mybook As Workbook
        Dim basebook As Workbook
        Dim CSVFileNames As Variant
        Dim SaveDriveDir As String
        Dim ExistFolder As Boolean
    
        'Save the current dir
        SaveDriveDir = CurDir
    
        'You can change the start folder if you want for
        'GetOpenFilename,you can use a network or local folder.
        'For example ChDirNet("C:\Users\Ron\test")
        'It now use Excel's Default File Path
    
        ExistFolder = ChDirNet("D:\GoogleDrive\Projects\Sericin rheology")
        If ExistFolder = False Then
            MsgBox "Error changing folder"
            Exit Sub
        End If
    
        CSVFileNames = Application.GetOpenFilename _
            (filefilter:="CSV Files (*.csv), *.csv", MultiSelect:=True)
    
        If IsArray(CSVFileNames) Then
    
            On Error GoTo CleanUp
    
            With Application
                .ScreenUpdating = False
                .EnableEvents = False
            End With
    
            'Add workbook with one sheet
            Set basebook = Workbooks.Add(xlWBATWorksheet)
    
            'Loop through the array with csv files
            For Fnum = LBound(CSVFileNames) To UBound(CSVFileNames)
    
                Set mybook = Workbooks.Open(CSVFileNames(Fnum))
    
                'Copy the sheet of the csv file after the last sheet in
                'basebook (this is the new workbook)
                mybook.Worksheets(1).Copy After:= _
                                          basebook.Sheets(basebook.Sheets.Count)
                On Error Resume Next
                ActiveSheet.Name = Right(CSVFileNames(Fnum), Len(CSVFileNames(Fnum)) - _
                                                InStrRev(CSVFileNames(Fnum), "\", , 1))
                On Error GoTo 0
    
                mybook.Close savechanges:=False
    
            Next Fnum
    
            'Delete the first sheet of basebook
            'On Error Resume Next
            'Application.DisplayAlerts = False
            'basebook.Worksheets(1).Delete
            'Application.DisplayAlerts = True
            'On Error GoTo 0
    
    CleanUp:
    
            ChDirNet SaveDriveDir
    
            With Application
                .ScreenUpdating = True
                .EnableEvents = True
            End With
        End If
    End Sub
    Last edited by JBeaucaire; 03-24-2016 at 05:02 PM. Reason: Added missing CODE tags. Please read and follow the Forum Rules, link above in the menu bar. Thanks.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Automating CSV imports to the same spreadsheet.

    Maybe like so:
            On Error Resume Next
            basebook.Sheets(1).Name = Right(CSVFilenames(Fnum), Len(CSVFilenames(Fnum)) - _
                                      InStrRev(CSVFilenames(Fnum), "\", , 1))
            On Error GoTo 0
    
            For Fnum = LBound(CSVFilenames) To UBound(CSVFilenames)
                Set mybook = Workbooks.Open(CSVFilenames(Fnum))
                If Fnum = LBound(CSVFilenames) Then
                    'Copy the sheet of the csv file after the last sheet in
                    'basebook (this is the new workbook)
                    mybook.Sheets(1).UsedRange.Copy basebook.Sheets(1).Range("A2")
                    basebook.Sheets(1).Range("B1").Value = CSVFilenames(Fnum)
                Else
                    mybook.Sheets(1).UsedRange.Offset(1).Copy basebook.Sheets(1).Cells(1, Columns.Count).End(xlToLeft).Offset(1, 1)
                    basebook.Sheets(1).Cells(2, Columns.Count).End(xlToLeft).Offset(-1).Value = CSVFilenames(Fnum)
                End If
                mybook.Close savechanges:=False
    
            Next Fnum
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 02-19-2014, 11:48 AM
  2. Automating Daily Data Imports from Web
    By hasanqz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-06-2013, 07:44 PM
  3. Help Automating Spreadsheet
    By buddyholly77 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-29-2012, 09:06 PM
  4. Automating Excel Spreadsheet
    By wallace23 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-25-2011, 09:59 PM
  5. Help With Spreadsheet-Automating?
    By craigf136 in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 04-21-2010, 06:26 AM
  6. Automating a spreadsheet with the use of dde
    By novice2430 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-30-2009, 04:58 PM
  7. automating a spreadsheet
    By devonkay in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-18-2009, 04:16 PM
  8. Automating Spreadsheet
    By cordin4 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 08-14-2008, 07:44 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1