+ Reply to Thread
Results 1 to 2 of 2

Get external data from a user selected .txt file

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-18-2012
    Location
    New York
    MS-Off Ver
    2016
    Posts
    193

    Get external data from a user selected .txt file

    Hello,

    I'm working on a macro to try and refresh a pivot table by clearing out the data table and importing a .txt file but I want a browser window to allow me to choose the file that I want to import. Any suggestions?

    Thanks again.

  2. #2
    Forum Contributor
    Join Date
    07-18-2012
    Location
    New York
    MS-Off Ver
    2016
    Posts
    193

    Re: Get external data from a user selected .txt file

    Found a solution that seems to work


    Option Explicit
    
    Sub Macro1()
    '
    ' Macro1 Macro
    '
    '
    Dim fName As String
    fName = Application.GetOpenFilename("Text Files (*.txt), *.txt")
    If fName = "False" Then Exit Sub
    '
    
    
        Sheets("msp_fee").Select
        Cells.Select
        Selection.QueryTable.Delete
        Selection.ClearContents
    
        With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & fName, _
            Destination:=Range("$A$1"))
            .Name = "msp_fee"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileOtherDelimiter = "|"
            .TextFileColumnDataTypes = Array(1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        Rows("1:7").Select
        Selection.Delete Shift:=xlUp
        Sheets("Sheet1").Select
        Range("A5").Select
        ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
    End Sub

+ 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. Trying to get value in user selected file
    By sehamm10 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-23-2013, 10:24 PM
  2. Can I refresh ONLY selected data from an external source?
    By hey_poolboy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-20-2013, 03:31 AM
  3. [SOLVED] Import user selected columns from a text file
    By aarho in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-11-2012, 08:28 PM
  4. How to extract file name from user selected file via Application.FileDialog
    By Jpdatabase in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-09-2012, 04:45 AM
  5. import data from .txt file selected by user
    By ahartman in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-29-2008, 03:34 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