+ Reply to Thread
Results 1 to 3 of 3

Issue with importing files

Hybrid View

  1. #1
    Registered User
    Join Date
    01-17-2017
    Location
    Brussels, Belgium
    MS-Off Ver
    2010
    Posts
    16

    Issue with importing files

    I need to import 2 files. First "File1"into the sheet 2 and second "File2" into the sheet 3. Both files are in the same folder. When I'm using following code for first file it works. I'm using this code also for the second file (of course for destination... instead of sheet2 there is always sheet3), but always import the first file in the folder. When I separate files into different folder, it works and I can import both files correctly. Do you have any idea how can be it solved?

    Sub choose_NEWfiles_for_comparison()
    
    Application.ScreenUpdating = False
          
        Dim FilesToOpen
        Dim sDelimiter As String
        Dim FirstRow As Long
    
        sDelimiter = " "
        
        FilesToOpen = Application.GetOpenFilename _
          (FileFilter:="Text Files (*.dat), *.dat", _
          Title:="Choose NEW File")
    
        If TypeName(FilesToOpen) = "Boolean" Then
            MsgBox "No Files were selected"
        End If
    
     sName = Dir(sPath & "*.dat")
        
    FirstRow = Range("A" & Rows.Count).End(xlUp).row
      
            With ActiveSheet.QueryTables.Add(Connection:= _
                "TEXT;" & sPath & sName, Destination:=Sheet2.Cells(FirstRow, "A"))
                .Name = Left(sName, Len(sName) - 4)
                .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 = 10
                .TextFileParseType = xlDelimited
                .TextFileTextQualifier = xlTextQualifierNone
                .TextFileConsecutiveDelimiter = True
                .TextFileTabDelimiter = True
                .TextFileSemicolonDelimiter = False
                .TextFileCommaDelimiter = False
                .TextFileSpaceDelimiter = True
                .TextFileOtherDelimiter = " "
                .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, _
                   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
                   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
                   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
                .TextFileTrailingMinusNumbers = True
                .Refresh BackgroundQuery:=False
                
            End With
    
    End Sub

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Issue with importing files

    Try this.

    Sub choose_NEWfiles_for_comparison()
    
    Application.ScreenUpdating = False
          
        Dim FilesToOpen
        Dim sDelimiter As String
        Dim FirstRow As Long
    
        sDelimiter = " "
        
        FilesToOpen = Application.GetOpenFilename _
          (FileFilter:="Text Files (*.dat), *.dat", _
          Title:="Choose NEW File")
    
        If TypeName(FilesToOpen) = "Boolean" Then
            MsgBox "No Files were selected"
            Exit Sub
        End If
        
        sName = Mid(FilesToOpen, InStrRev(FilesToOpen, "\") + 1)
            
        FirstRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
      
            With ActiveSheet.QueryTables.Add(Connection:= _
                "TEXT;" & FilesToOpen, Destination:=ActiveSheet.Cells(FirstRow, "A"))
                .Name = Left(sName, Len(sName) - 4)
                .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 = 10
                .TextFileParseType = xlDelimited
                .TextFileTextQualifier = xlTextQualifierNone
                .TextFileConsecutiveDelimiter = True
                .TextFileTabDelimiter = True
                .TextFileSemicolonDelimiter = False
                .TextFileCommaDelimiter = False
                .TextFileSpaceDelimiter = True
                .TextFileOtherDelimiter = " "
                .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, _
                   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
                   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
                   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
                .TextFileTrailingMinusNumbers = True
                .Refresh BackgroundQuery:=False
                
            End With
    
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    01-17-2017
    Location
    Brussels, Belgium
    MS-Off Ver
    2010
    Posts
    16

    Re: Issue with importing files

    Many Many thanks it's working perfectly

+ 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. Importing CSV files to Access 2007 - 15+ digit issue!
    By beasley101 in forum Access Programming / VBA / Macros
    Replies: 3
    Last Post: 09-26-2015, 07:01 AM
  2. [SOLVED] IMPORTING TEXT FILES TO EXCEL - Multiple Multi-line text files, each in a new column
    By wrenchfry in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-23-2014, 08:37 PM
  3. Replies: 0
    Last Post: 11-27-2012, 01:43 PM
  4. [SOLVED] Importing Multiple Files to Single Workbook date issue
    By kevinbowe2000 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-25-2012, 07:41 AM
  5. Importing XML issue
    By allin1joe in forum Excel General
    Replies: 2
    Last Post: 03-23-2011, 03:43 PM
  6. Importing files .csv files from yahoo finance.
    By larryg003 in forum Excel General
    Replies: 1
    Last Post: 09-08-2010, 09:52 PM
  7. [SOLVED] Importing from Access issue
    By truss in forum Excel General
    Replies: 1
    Last Post: 05-20-2005, 10:06 AM

Tags for this Thread

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