Consulting

Results 1 to 7 of 7

Thread: Solved: import multiple text files from listbox

  1. #1

    Solved: import multiple text files from listbox

    Hi all
    this code dumps a list of .dat file names into a list box.
    it is a multiselect list box with check boxes.

    what i am trying to do is when i check multiple items in the list it will import the text file/s into the current workbook,leaving me with a sheet for each imported text file

    any ideas would be helpful.
    cheers

    [vba]Public Sub CommandButton11_Click()
    With Application.FileSearch
    .NewSearch
    .LookIn = "C:\Documents and Settings\Dan\My Documents\Copy of Sapflow"
    .SearchSubFolders = True
    .Filename = "*.dat"
    If .Execute() > 0 Then
    For i = 1 To .FoundFiles.Count
    WSUD.ListBox1.AddItem Dir(.FoundFiles(i), vbDirectory)
    Next i
    Else
    MsgBox "There were no files found."
    End If
    End With
    end sub[/vba]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub cmdOK_Click()
    Dim i As Long
    Dim First As Boolean
    Dim wb As Workbook
    First = True
    With ListBox1
    For i = 0 To .ListCount - 1
    If .Selected(i) Then
    Workbooks.Open .List(i)
    If First Then
    First = False
    Set wb = ActiveWorkbook
    Else
    ActiveWorkbook.Worksheets(1).Move After:=wb.Worksheets(wb.Worksheets.Count)
    End If
    End If
    Next i
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    The problem i have is that the selected item needs to have a path as in [vba].foundfiles(i) 'This includes the full pathh[/vba]
    however
    [vba]Dir(FoundFiles(i),vbdirectory) 'this trims the file path for display in the listbox
    [/vba]

    The only thing i have come up with is to add a collection of the original foundfiles with full path and somhow connect them on selection.


    [vba]Private mycollection1 As Collection
    Public Sub CommandButton11_Click()
    Set mycollection1 = New Collection
    With Application.FileSearch
    .NewSearch
    .LookIn = "C:\Documents and Settings\Dan\My Documents\Copy of Sapflow"
    .SearchSubFolders = True
    .Filename = "*.dat"
    If .Execute() > 0 Then
    For i = 1 To .FoundFiles.Count

    mycollection1.Add .FoundFiles(i)
    WSUD.ListBox1.AddItem Dir(.FoundFiles(i), vbDirectory)
    Next i
    Else
    MsgBox "There were no files found."
    End If
    End With[/vba]

    so far i have only come up with one monstosity of tangled code after another, even going as far as this below
    [vba]Option Compare Text
    Private Sub CommandButton56_Click()
    With Listbox1
    For x = 0 To ListBox1.ListCount
    If .Selected(x) Then
    For Each i In mycollection1
    mystr = Right(i, (Len(i) - InStrRev(i, "\")))
    If mystr = .List(x) Then
    Debug.Print .List(x)' this is were i would like to import the file with its path from collection1
    End If
    Next i
    End If
    Next x
    End With[/vba]

    [vba]mystr = Right(i, (Len(i) - InStrRev(i, "\"))) ' this is actually trying to temporarily manipulate the file path for a comparison[/vba]
    So far as you can see it is a mess and cannot work out how to associate these values.
    As you can see the main problem is that once the item is imported into the listbox it loses it path and only exists as a filename,which in turn will not open the file.

    Any ideas? maybe i am going around this the wrong way? it seems i amtrying to fit a circle in to a square shaped hole.

    Cheers

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You know the path as you use that ijn the Lookin value of the FileSearch, so just save that in a variable and pre-prend it to the Listbox selection when needed.

    BTW, FileSearch has been dropped in Excel 2007.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    thanks mate,

    The problem is that the filesearch searches subfolders which means there is at least 5 paths depending on which subfolder the file resides in.
    This is good for me as I dont have to have a set path and that filesearch retreives the path for each seperate file, however using a listbox this way is decidedly difficult.

    I will keep looking into this
    thanks for the post its been helpful

    cheers

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Oh yeah, didn't spot that.

    I would add a nother column to the listb ox then, don't display it though, with the full path

    [vba]

    WSUD.ListBox1.List(WSUD.ListBox1.ListCount - 1, 1) = FoundFiles(i)
    [/vba]

    and retrive that value when accessing the file


    [vba]

    .List(x, 1)
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    thanks
    never thought of another column.
    this even works if extra items are added to the list.

    cheers

Posting Permissions

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