+ Reply to Thread
Results 1 to 11 of 11

Let user select file to open

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-14-2005
    Location
    Sweden
    MS-Off Ver
    Office 365
    Posts
    329

    Re: Let user select file to open

    Quote Originally Posted by Richard Buttrey View Post
    Hi

    Sub YourSub
    
    Dim stFName as String
    stFName = Application.GetOpenFileName
    
    With ActiveSheet.QueryTables.Add(Connection:= _
            stFName, _
            Destination:=Range("$A$1"))
    '....etc
    It seems to work well until the ....etc code starts.
    I have written this way:
    Dim stFName As String
    stFName = Application.GetOpenFilename
    Sheets("Data").Select
    
    With ActiveSheet.QueryTables.Add(Connection:= _
            stFName, _
            Destination:=Range("$A$1"))
            .Name = "data_267798_7267"        
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
    What is marked in red text is causing a problem since it is hardcoded filename without the extension.
    Can I extract the filenamn from the stFName in some way?

    /Anders

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Let user select file to open

    Are you saying that name is in some way related to the filename? If so what's a typical filename and what do you want that .Name string to be for that example file name.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor
    Join Date
    03-14-2005
    Location
    Sweden
    MS-Off Ver
    Office 365
    Posts
    329

    Re: Let user select file to open

    In the original code:
    With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;C:\Users\Me\Google Drive\Our team\data_267798_7267.csv", _
            Destination:=Range("$A$1"))
            .Name = "data_267798_7267"
    How can I get the filename into the .data-field?
    It need to be the same filename as the imported filename.

    Anders

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Let user select file to open

    Hi,

    In much the same way as before but with an extra variable

    Dim stFName As String, stfPath As String
    stfPath = Application.GetOpenFilename
    stFName = Left(StrReverse(Split(StrReverse(stfPath), "\")(0)), InStr(StrReverse(stfPath), "."))
    Sheets("Data").Select
    
    With ActiveSheet.QueryTables.Add(Connection:= _
            stfPath, _
            Destination:=Range("$A$1"))
            .Name = stFName
    
    'etc...

  5. #5
    Forum Contributor
    Join Date
    03-14-2005
    Location
    Sweden
    MS-Off Ver
    Office 365
    Posts
    329

    Re: Let user select file to open

    Quote Originally Posted by Richard Buttrey View Post
    Hi,

    In much the same way as before but with an extra variable

    Dim stFName As String, stfPath As String
    stfPath = Application.GetOpenFilename
    stFName = Left(StrReverse(Split(StrReverse(stfPath), "\")(0)), InStr(StrReverse(stfPath), "."))
    Sheets("Data").Select
    
    With ActiveSheet.QueryTables.Add(Connection:= _
            stfPath, _
            Destination:=Range("$A$1"))
            .Name = stFName
    
    'etc...
    Yes, I believe it should be this simple but my knowledge is very limited.
    I still get an error though. "Application defined or object-defined error"

    Dim stFName As String, stfPath As String
    Sheets("Data").Select
    
    'Hämta filnamnet utan extension
    stfPath = Application.GetOpenFilename
    
    'Get the name of the file without the file extension. (Make:data_267798_7267.csv become, data_267798_7267)
    stFName = Left(StrReverse(Split(StrReverse(stfPath), "\")(0)), InStr(StrReverse(stfPath), "."))
    
    With ActiveSheet.QueryTables.Add(Connection:= _
            stfPath, _
            Destination:=Range("$A$1"))        
            .Name = stFName
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
    Any thoughts?

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Let user select file to open

    Difficult to say without seeing the workbook if this element was working before. Would you upload the workbook?

  7. #7
    Forum Contributor
    Join Date
    03-14-2005
    Location
    Sweden
    MS-Off Ver
    Office 365
    Posts
    329

    Re: Let user select file to open

    There are some differences from the orignial code:
    With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;C:\Users\Me\Google Drive\Our team\data_267798_7267.csv", _
            Destination:=Range("$A$1"))
            .Name = "data_267798_7267"
    Your code:
    With ActiveSheet.QueryTables.Add(Connection:= _
            stfPath, _
            Destination:=Range("$A$1"))
    The parameter stfName = Returns only "data". Not data_267798_7267 so there is something wrong there as well.


    Attachments

    Here is the data file I am trying to import.
    data_267798_7267.csv
    Here is the xlsm file that imports the file. It is unfortunately in swedish. On the second slide There is a macro button that starts the import. (1.Hämta Data)
    Orderhandling.zip

+ 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] Userform that allows user to select an open file?
    By anon in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-01-2014, 05:25 AM
  2. [SOLVED] Macro that prompts user to select a file to open and perform another macro on this file
    By grimmy26 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-16-2014, 08:39 AM
  3. [SOLVED] Open specific folder, select file to open and copy then paste
    By Kranky in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-22-2012, 12:14 AM
  4. Not able to open or select other work books while user form is activate
    By rvc81 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-16-2011, 06:14 AM
  5. Replies: 12
    Last Post: 06-07-2011, 04:17 AM

Tags for this Thread

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