ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Importing multiple files (https://www.excelbanter.com/excel-programming/430502-importing-multiple-files.html)

Rick S.

Importing multiple files
 

I borrowed a majority of this code from this NG and modified (read destroyed)
it for my needs, now I can't figure what I did wrong.
The macro should import a text file starting in A1, get the last row +1
(counting in column B) and repeat untill all files in the folder are
processed. The only cloumns that should have data are A thru H, row count
will vary.

Initially it appears to work, then the macro starts moving data to other
columns, like taking A1 thru A867 (A thru H) and moving it to I thru P and so
on and adding blank rows from the top down?

Code:

Sub aaa()
'Set Folder path
    Folder = "C:\MEASURE-6000\OUTPUT\Test\"

    'Create worksheet "Input"
    With ThisWorkbook
        Set InputSht = Worksheets.Add( _
                      after:=.Sheets(.Sheets.Count))
        InputSht.Name = "Input"
    End With
    'File name Extension to find
    FName = Dir(Folder & "*.OUT")

    Do While FName < ""
        'Input data file
        With InputSht
            'Count rows in column B
            InputShtLastRow = .Cells(Rows.Count, "B").End(xlUp).Row
            'MsgBox InputShtLastRow    'for testing

            'Add data to worksheet and append each new import
            With .QueryTables.Add( _
                Connection:="TEXT;" & Folder & FName, _
                Destination:=.Range("A" & InputShtLastRow + 1))

                .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 = 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
        End With
    Loop
End Sub

Please help, I only have six or seven brain cells left!

--
Regards

XP Pro
Office 2003

Rick S.

Importing multiple files
 

I think I put myself in an endless loop with *Do While FName < ""*

--
Regards

XP Pro
Office 2003


"Rick S." wrote:

I borrowed a majority of this code from this NG and modified (read destroyed)
it for my needs, now I can't figure what I did wrong.
The macro should import a text file starting in A1, get the last row +1
(counting in column B) and repeat untill all files in the folder are
processed. The only cloumns that should have data are A thru H, row count
will vary.

Initially it appears to work, then the macro starts moving data to other
columns, like taking A1 thru A867 (A thru H) and moving it to I thru P and so
on and adding blank rows from the top down?

Code:

Sub aaa()
'Set Folder path
    Folder = "C:\MEASURE-6000\OUTPUT\Test\"

    'Create worksheet "Input"
    With ThisWorkbook
        Set InputSht = Worksheets.Add( _
                        after:=.Sheets(.Sheets.Count))
        InputSht.Name = "Input"
    End With
    'File name Extension to find
    FName = Dir(Folder & "*.OUT")

    Do While FName < ""
        'Input data file
        With InputSht
            'Count rows in column B
            InputShtLastRow = .Cells(Rows.Count, "B").End(xlUp).Row
            'MsgBox InputShtLastRow    'for testing

            'Add data to worksheet and append each new import
            With .QueryTables.Add( _
                  Connection:="TEXT;" & Folder & FName, _
                  Destination:=.Range("A" & InputShtLastRow + 1))

                .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 = 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
        End With
    Loop
End Sub


Please help, I only have six or seven brain cells left!

--
Regards

XP Pro
Office 2003



All times are GMT +1. The time now is 09:57 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com