Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 31st, 2007, 10:49 AM
Registered User
Join Date: Jul 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Text import - Browse for location

Hello all,
I am trying to import a text file onto Excel, and I would like the user to point where the file is:

With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\HERE_IT_IS.txt", _
        .Name = "1.RNC_EXPORT"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True

I would like to add a command that throws a browser window where the user can click on and locate the file himself. Like the "File - Open" command in excel.

Is this doable?


Old August 1st, 2007, 11:59 AM
Friend of Wrox
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts

This is relatively easy.

Make a command button called GetFile_Button as below:
Private Sub GetFile_Button_Click()

  Dim sFile As String, fsDialog As FileDialog
  Set fsDialog = Application.FileDialog(msoFileDialogFilePicker)
  With fsDialog
    .Title = "Select txt file to open"
    .Filters.Add "Text Files", "*.txt", 1
    If .SelectedItems.Count < 1 Then sFile = "" Else sFile = .SelectedItems(1)
  End With
  MsgBox sFile

End Sub

You'll have to trap the value(s) yourself and determine if it's an appropriate file or not. I find .InitialFileName useful to trim off the path.

Hope this helps.

Old August 2nd, 2007, 05:18 AM
Registered User
Join Date: Jul 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts

Hello allenm

Thank you very much for your response. Much appreciated.

As for the file check, I'll train my users to do it themselves.

Again, it was very helpful ! Thanks

Similar Threads
Thread Thread Starter Forum Replies Last Post
Connection/Command Error Text Location BrianWren Visual Basic 2005 Basics 1 September 8th, 2008 06:00 AM
How to change text on "Browse" button? Sagar215 Java GUI 1 January 20th, 2008 06:19 PM
Text Import- How to import the first 5 lines. nickzhang.zn Excel VBA 1 August 4th, 2007 05:06 PM
Set cursor at specific location in text box pritz VB How-To 3 August 29th, 2005 10:27 AM
Set cursor at specific location in text box pritz Beginning VB 6 1 August 15th, 2005 12:22 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.