Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 31st, 2007, 10:49 AM
Registered User
 
Join Date: Jul 2007
Location: , , .
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:

Code:
With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\HERE_IT_IS.txt", _
        Destination:=Range("A1"))
        .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?

Cheers


Reply With Quote
  #2 (permalink)  
Old August 1st, 2007, 11:59 AM
Friend of Wrox
Points: 513, Level: 8
Points: 513, Level: 8 Points: 513, Level: 8 Points: 513, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2007
Location: Davenport, IA, USA.
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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
    .Show
    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.

Reply With Quote
  #3 (permalink)  
Old August 2nd, 2007, 05:18 AM
Registered User
 
Join Date: Jul 2007
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:31 AM.


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