Results 1 to 2 of 2

Append many imported files to a worksheet

Threaded View

  1. #1
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177

    Append many imported files to a worksheet

    In the modified code below I am trying to import text files (.OUT) and append each file by rows onto sheet 4.
    What I get is each file plastered about the worksheet.
    Some of the data is properly entered in columns "A thru H" and some of the files append rows within the same columns while others are placed in columns "I thru P" in the same rows and others are further out in the columns.

    Option Explicit
    
    'Bill Manville's modified code
    Sub ImportFilesInFolder()
    Dim aFiles() As String, iFile As Integer
    Dim stFile As String, vFile As Variant
    Dim stDirectory As String
    Dim stCode As String
    Dim sMyRange As String
    Dim Sh4LastRow As String
    Dim Sh4Range As Range
    
        ' first build an array of the files and then process them
        ' this is because you may upset the Dir function if you save a file
    
    stDirectory = "C:\MEASURE-6000\OUTPUT\"  ' name of directory to look in
        ' use Dir function to find XLS files in Directory
        stFile = Dir(stDirectory & "*.OUT")
        If stFile = "" Then Exit Sub    ' no files to process
        Do While stFile <> ""
            ' add to array of files
            iFile = iFile + 1
            ' add one element to the array
            ReDim Preserve aFiles(1 To iFile)
            aFiles(iFile) = stFile
            stFile = Dir()   ' gets next file
        Loop
        'MsgBox iFile    ' for testing
    
        With Sheets("Sheet4")    'Loop thru Column C
            Sh4LastRow = .Cells(Rows.Count, "C").End(xlUp).Row
            Set Sh4Range = .Range("C1:C" & Sh4LastRow)
    
            If .Range("A1").Value = "" Then
                sMyRange = "A" & Sh4LastRow + 1
                'MsgBox sMyRange 'for testing
                ' process the files
                For Each vFile In aFiles
                    'MsgBox stDirectory & "\" & vFile    'for testing, actual file name
                    'Add data to worksheet and append each new _
                    import in columns A thru H
                    With .QueryTables.Add( _
                         Connection:="TEXT;" & stDirectory & vFile, _
                         Destination:=.Range(sMyRange))
                        .FieldNames = True
                        .RowNumbers = False
                        .FillAdjacentFormulas = False
                        .PreserveFormatting = True
                        .RefreshOnFileOpen = False
                        .RefreshStyle = xlInsertDeleteCells
                        .SavePassword = False
                        .SaveData = True
                        .AdjustColumnWidth = True
                        .RefreshPeriod = 0
                        .TextFilePromptOnRefresh = False
                        .TextFilePlatform = xlWindows
                        .TextFileStartRow = Sh4LastRow + 1
                        .TextFileParseType = xlDelimited
                        .TextFileTextQualifier = xlTextQualifierDoubleQuote
                        .TextFileConsecutiveDelimiter = True
                        .TextFileTabDelimiter = True
                        .TextFileSemicolonDelimiter = False
                        .TextFileCommaDelimiter = True
                        .TextFileSpaceDelimiter = True
                        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1)
                        .Refresh BackgroundQuery:=False
                    End With
                Next vFile
            End If
        End With
    End Sub
    Any hints, tips and or examples are appreciated.
    See attached excel file of sheet 4.
    Attached Files Attached Files
    Last edited by Rick_Stanich; 07-01-2009 at 12:45 PM.
    Regards

    Rick
    Win10, Office 365

Thread Information

Users Browsing this Thread

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

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