+ Reply to Thread
Results 1 to 21 of 21

Importing a text file by vba referring to a cell for file name & file path

  1. #1
    Forum Contributor
    Join Date
    02-13-2016
    Location
    MUMBAI
    MS-Off Ver
    Microsoft 365
    Posts
    180

    Importing a text file by vba referring to a cell for file name & file path

    hello everyone!!

    I want to import a text file for which the vba code should refer to the cell B1 for File Path & cell B2 for File name.

    My relevant extract of the vba code is as under.

    Please Login or Register  to view this content.
    Please if anyone can help me out of this.

    Thanks.

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,519

    Re: Importing a text file by vba referring to a cell for file name & file path

    Perhaps
    If path in B1 has path separator at the end or you need to add to it.
    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    02-13-2016
    Location
    MUMBAI
    MS-Off Ver
    Microsoft 365
    Posts
    180

    Re: Importing a text file by vba referring to a cell for file name & file path

    Hi Jindon,

    I dont have much knowledge of the vba but i just can record the macro well.

    I jus copy pasted your above code and got the syntax error. I am not able to figure out the error..

    Provide me the exact code which i just need to copy paste in place of my bold text in my code mentioned above.

    Thanks.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,519

    Re: Importing a text file by vba referring to a cell for file name & file path

    What are values in B1 & B2?

  5. #5
    Forum Contributor
    Join Date
    02-13-2016
    Location
    MUMBAI
    MS-Off Ver
    Microsoft 365
    Posts
    180

    Re: Importing a text file by vba referring to a cell for file name & file path

    Thanks for the quick response jindon.

    A B
    1 Text File Path : C:\Users\vivek\Downloads\FORM 26AS PROMPT
    2 Text File Name : AAAFP8402D-2016

    i.e. cell b1: C:\Users\vivek\Downloads\FORM 26AS PROMPT
    & cell b2: AAAFP8402D-2016

    Its like this..Path & File name will change with the user & hence i wanted to link the vba to the cell value.
    Last edited by Vivek2705; 10-15-2017 at 03:00 AM.

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,519

    Re: Importing a text file by vba referring to a cell for file name & file path

    So no path separator at the end.
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    02-13-2016
    Location
    MUMBAI
    MS-Off Ver
    Microsoft 365
    Posts
    180

    Re: Importing a text file by vba referring to a cell for file name & file path

    vba error screenshot.png

    sent you the vba error screenshot

  8. #8
    Forum Contributor
    Join Date
    02-13-2016
    Location
    MUMBAI
    MS-Off Ver
    Microsoft 365
    Posts
    180

    Re: Importing a text file by vba referring to a cell for file name & file path

    Excel file cell b1 & b2.png

    screenshot of cell b1 & b2 values

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,519

    Re: Importing a text file by vba referring to a cell for file name & file path

    Try change to
    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    02-13-2016
    Location
    MUMBAI
    MS-Off Ver
    Microsoft 365
    Posts
    180

    Re: Importing a text file by vba referring to a cell for file name & file path

    screenshot.png

    .Refresh BackgroundQuery:=False was highlighted in yellow after i pressed debug button.

  11. #11
    Forum Contributor
    Join Date
    02-13-2016
    Location
    MUMBAI
    MS-Off Ver
    Microsoft 365
    Posts
    180

    Re: Importing a text file by vba referring to a cell for file name & file path

    Please Login or Register  to view this content.
    This is the entire vba code which i have recorded.

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,519

    Re: Importing a text file by vba referring to a cell for file name & file path

    That would be something like the file you specified is not found.

  13. #13
    Forum Contributor
    Join Date
    02-13-2016
    Location
    MUMBAI
    MS-Off Ver
    Microsoft 365
    Posts
    180

    Re: Importing a text file by vba referring to a cell for file name & file path

    It does exist..without giving reference to a cell for path & file name the code runs well..

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,519

    Re: Importing a text file by vba referring to a cell for file name & file path

    You are adding new sheet at the top then how is it possible to have value in B1 & B2???

    They should be blank.

  15. #15
    Forum Contributor
    Join Date
    02-13-2016
    Location
    MUMBAI
    MS-Off Ver
    Microsoft 365
    Posts
    180

    Re: Importing a text file by vba referring to a cell for file name & file path

    Ohh now i got the issue.. I want to refer the path & file name (cell b1 & b2) from sheet named "main sheet"..
    So what changes should be made in the vba accordingly??

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,519

    Re: Importing a text file by vba referring to a cell for file name & file path

    Add 2 lines and change a part.
    Please Login or Register  to view this content.

  17. #17
    Forum Contributor
    Join Date
    02-13-2016
    Location
    MUMBAI
    MS-Off Ver
    Microsoft 365
    Posts
    180

    Re: Importing a text file by vba referring to a cell for file name & file path

    Untitled.png

    I think the code still searches for path in cell B1 of the new sheet inserted and not the "Main Sheet" which already exists

  18. #18
    Forum Contributor
    Join Date
    02-13-2016
    Location
    MUMBAI
    MS-Off Ver
    Microsoft 365
    Posts
    180

    Re: Importing a text file by vba referring to a cell for file name & file path

    Heyy Jindon & everyone,

    Attached are the excel & Text file for your reference...

    kindly provide the solution to it..

    Thanks.
    Last edited by Vivek2705; 12-01-2017 at 08:43 AM.

  19. #19
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,519

    Re: Importing a text file by vba referring to a cell for file name & file path

    Replace bold line above with the below
    Please Login or Register  to view this content.

  20. #20
    Forum Contributor
    Join Date
    02-13-2016
    Location
    MUMBAI
    MS-Off Ver
    Microsoft 365
    Posts
    180

    Re: Importing a text file by vba referring to a cell for file name & file path

    Thankss a lot Jindon...it worked finally....

  21. #21
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,519

    Re: Importing a text file by vba referring to a cell for file name & file path

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

+ 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. Populate file path and file name in save as dialog box using text in worksheet
    By runnerD2016 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-19-2016, 05:04 PM
  2. Importing text file. Does entire file get read into memory first?
    By sumdumgai in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-11-2016, 07:28 PM
  3. Replies: 4
    Last Post: 06-16-2015, 12:06 PM
  4. VBA Importing Text File makes the file longer to load everytime
    By Hudas in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-15-2014, 11:26 AM
  5. Importing a data from a text file - user-defined file
    By DaveSev in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-01-2013, 07:02 PM
  6. Replies: 0
    Last Post: 01-27-2013, 12:13 PM
  7. [Ask] Import Text file And Call reference path file with Value Range.
    By dha_indra in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-29-2012, 11:09 PM

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