+ Reply to Thread
Results 1 to 2 of 2

Importing a data from a text file - user-defined file

Hybrid View

  1. #1
    Registered User
    Join Date
    12-13-2012
    Location
    Ohio
    MS-Off Ver
    Excel 2007
    Posts
    7

    Importing a data from a text file - user-defined file

    Hey all,

    I need to make a macro that imports text data from a file in a user-defined directory.

    The data will always be from a different directory but will always be in the same format. The data is a text file that we've changed the extension to for other purposes, something like .datatxt.

    What I would like to do is have the user enter in the correct directory, push a button, and the script pulls the data in from the proper file in that directory.

    The data files will be something like

    Q:\.........\Results\Test#_####.output.datatxt

    and it will be the only file in the directory with the Results\.....output.datatxt name. So I have no problem getting the full file name using some concatenate/len/right functions once the user inputs the directory.

    Just need to know how to make the script search for that file.

    Thanks!
    Last edited by DaveSev; 04-01-2013 at 06:47 PM.

  2. #2
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Importing a data from a text file - user-defined file

    This macro will ask the user to select the directory and find the file. You just have to add your code to get data from that file. As noted this macro is part of a macro published by jindon on this forum.
    Sub Dir_Select() 'From jindon, a guru from this forum
        Dim myDir As String, fn As String, txt As String, n As Long
        With Application.FileDialog(msoFileDialogFolderPicker)
            If .Show Then myDir = .SelectedItems(1)
        End With
        If myDir = "" Then Exit Sub
        fn = Dir(myDir & "\*.datatxt")
        If fn = "" Then
            MsgBox "no .txt files found"
            Exit Sub
        End If
    'If you want to find the next file in this directory (if needed)
    'uncomment next line
    'fn= dir
    Pierre Leclerc
    _______________________________________________________

    If you like the help you got,
    Click on the STAR "Add reputation" icon at the bottom.

+ Reply to Thread

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