Consulting

Results 1 to 5 of 5

Thread: Import rules for text files

  1. #1
    VBAX Contributor Airborne's Avatar
    Joined
    Oct 2004
    Location
    Rotterdam
    Posts
    147
    Location

    Import rules for text files

    Hello, A while ago I got a great code for looking for a file in a directory and importing it in Excel

    Sub Import()
        If Sheets("Sheet1").[A1].Value = "" Then Exit Sub
        Dim fName As String
        Dim mybook As Workbook
        fName = Sheets("Sheet1").[A1].Value & ".txt"
        On Error Resume Next
        Set mybook = Workbooks(fName)
        On Error GoTo 0
        If mybook Is Nothing Then
            With Application.FileSearch
                .NewSearch
                .LookIn = "\\Server\submap\"
                .SearchSubFolders = True
                .Filename = fName
                If .Execute > 0 Then
                    Workbooks.Open (.FoundFiles(1))
                                       Else
                    MsgBox fName & " not found!"
                    End
                End If
             End With
        Else
            MsgBox fName & " Is already open!"
        End If
    End Sub
    My problem is that the text file I want to import has to come with this line
    Origin:= xlWindows, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:= _
            Array(Array(0, 1), Array(8, 1), Array(21, 1), Array(34, 1), Array(46, 1), _
            Array(60, 1), Array(63, 1), Array(66, 1))

    For a better view of the sheet. Where can I put the line in the import code? I've tried to put the line behind Workbooks.Open (.FoundFiles(1)) but it doesn't work

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Try changing
    Workbooks.Open (.FoundFiles(1))
    To:

    Workbooks.Opentext filename:=.FoundFiles(1),Origin:= xlWindows, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:= _
    Array(Array(0, 1), Array(8, 1), Array(21, 1), Array(34, 1), Array(46, 1), _
    Array(60, 1), Array(63, 1), Array(66, 1))
    I haven't tested this, but I don't think you need the extra brackets around the .foundfiles(1) componenent of the code. If it doesn't work, try adding those, and if still not, post back.

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    VBAX Contributor Airborne's Avatar
    Joined
    Oct 2004
    Location
    Rotterdam
    Posts
    147
    Location
    Great, thanks. You are right, I don't need the extra brackets and the code works.

  4. #4
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Glad to help!

    Not sure if you are aware, but at the top of the post, in the "Thread Tools" menu, you can mark this as solved.

    Cheers,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  5. #5
    VBAX Contributor Airborne's Avatar
    Joined
    Oct 2004
    Location
    Rotterdam
    Posts
    147
    Location
    I didn't know that, thanks again

Posting Permissions

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