+ Reply to Thread
Results 1 to 5 of 5

Get VBA to allow user to Browse for a file to open

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-13-2010
    Location
    England
    MS-Off Ver
    Office 2016 365
    Posts
    211

    Get VBA to allow user to Browse for a file to open

    Hi,

    I am very, VERY new to VBA so apologies if this is general knowledge.

    I am writing a macro that takes a dump of data in a text file, converts it to columns in excel and copies it to an Access Database.

    My code for opening the text file is simply

    Dim TxtFileName as String
    TxtFileName = "C:\Users\7092\Desktop\Current Work\sqlexec.txt"
    Workbooks.OpenText Filename:=TxtFileName, Origin:=437, StartRow:=1, 
    DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, 
    ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, 
    Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1)), 
    DecimalSeparator:=".", ThousandsSeparator:=",", TrailingMinusNumbers:=True
    However, what i would like is for the user to be able to browse to find the text file and if it isn't too much trouble, for a warning to be presented if they try to choose a file that isn't a txt file.

    Thanks in advance for any help you can give me.

    J

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Get VBA to allow user to Browse for a file to open

    This thread is a duplicate.

  3. #3
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Get VBA to allow user to Browse for a file to open

    Duplicate post
    Last edited by JapanDave; 06-28-2012 at 05:40 AM. Reason: Duplicate post, due to damm faulty database.
    Be fore warned, I regularly post drunk. So don't take offence (too much) to what I say.
    I am the real 'Napster'
    The Grid. A digital frontier. I tried to picture clusters of information as they moved through the computer. What did they look like? Ships? motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I'd never see. And then, one day...

    If you receive help please give thanks. Click the * in the bottom left hand corner.

    snb's VBA Help Files

  4. #4
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Get VBA to allow user to Browse for a file to open

    This code will only allow you to choose .txt files, but I put a bit of code there anyway to show you what you could do if you allow multiple file ext.

    Sub dave()
    
     
        Path = Application.GetOpenFilename("txt Files (*.txt), *.txt", , "Select Text File")
        If Path = "False" Then
            GoTo ExitSub:
        End If
         If Right(Path, 4) <> ".txt" Then
           MsgBox "Please select text file."
            GoTo ExitSub
         End If
        
        Workbooks.Open Filename:=Path
    ExitSub:
    End Sub
    Last edited by JapanDave; 06-28-2012 at 05:39 AM.

  5. #5
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Get VBA to allow user to Browse for a file to open

    Didn't see that, thanks Steven.

+ 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