+ Reply to Thread
Results 1 to 3 of 3

How to insert FileDialog option into recorded code

Hybrid View

  1. #1
    Registered User
    Join Date
    09-18-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    11

    How to insert FileDialog option into recorded code

    Hi All

    Recorded the following code to import a text file, but how can I insert a FileDialog box so I can manually select the file I want to import.

    Thanks for the assistance.

    Sub Import_Text_File()
    '
    ' Import_Text_File Macro
    '
    
    '
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;C:\Desktop\TextFile\TestText.txt" _
            , Destination:=Range("$A$1"))
            .Name = "TestText_1"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 850
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
    End Sub

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,749

    Re: How to insert FileDialog option into recorded code

    Can't test this without your files, but I tested the file name retrieval part:
    Sub Import_Text_File()
    '
    ' Import_Text_File Macro
    '
       Dim FD As FileDialog
       Dim FilePath As String
       
       Set FD = Application.FileDialog(msoFileDialogFilePicker)
       FD.Title = "Please select file"
       FD.Filters.Clear
       FD.Filters.Add "txt files", "*.txt"
       
       If FD.Show = -1 Then
          FilePath = FD.SelectedItems(1)
          
          With ActiveSheet.QueryTables.Add(Connection:= _
          "TEXT;" & FilePath _
             , Destination:=Range("$A$1"))
             .Name = "TestText_1"
             .FieldNames = True
             .RowNumbers = False
             .FillAdjacentFormulas = False
             .PreserveFormatting = True
             .RefreshOnFileOpen = False
             .RefreshStyle = xlInsertDeleteCells
             .SavePassword = False
             .SaveData = True
             .AdjustColumnWidth = True
             .RefreshPeriod = 0
             .TextFilePromptOnRefresh = False
             .TextFilePlatform = 850
             .TextFileStartRow = 1
             .TextFileParseType = xlDelimited
             .TextFileTextQualifier = xlTextQualifierDoubleQuote
             .TextFileConsecutiveDelimiter = False
             .TextFileTabDelimiter = True
             .TextFileSemicolonDelimiter = False
             .TextFileCommaDelimiter = False
             .TextFileSpaceDelimiter = False
             .TextFileColumnDataTypes = Array(1, 1, 1, 1)
             .TextFileTrailingMinusNumbers = True
             .Refresh BackgroundQuery:=False
          End With
       
       End If
    
    End Sub
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    09-18-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: How to insert FileDialog option into recorded code

    Many Thanks

    Updated code worked perfectly.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] How to add INSERT ROW option with below code.
    By rajeshn_in in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-27-2016, 09:51 AM
  2. Unable to incorporate a recorded code in a bigger code
    By VKS in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-02-2016, 02:14 PM
  3. Use code instead of recorded macro
    By harman83 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-11-2015, 10:17 AM
  4. Replies: 6
    Last Post: 08-14-2015, 03:34 PM
  5. Replace Macro recorded code with more effective code
    By scaffdog845 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-12-2014, 01:09 AM
  6. Simplify recorded code
    By tanksalevikrant in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-01-2007, 07:45 AM
  7. Mod macro recorded code
    By usr789 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-27-2007, 09:27 AM

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