+ Reply to Thread
Results 1 to 19 of 19

Change code to an input box to enter the file name

  1. #1
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Change code to an input box to enter the file name

    Hi all,

    I have some code which i have tested and is working great, but i would like to change it slightly so that i can input a file name so it picks the correct .xls workbook.

    in my code at the minute it is looking for "sample_BOM" i would like to be able to enter the file name manual every time, i have commented the code so you can see where it is.

    Please Login or Register  to view this content.
    I would also like to only open the userform when a button is pressed in the excel worksheet and not automatically when the workbook is opened. I have this macro attached to a button which opens the user form once the BOM is open.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Change code to an input box to enter the file name

    To have the userform not open when worksheet is activated, remove this line
    Please Login or Register  to view this content.
    from
    Please Login or Register  to view this content.
    Then put this in your button on the worksheet
    Please Login or Register  to view this content.
    i.e
    Please Login or Register  to view this content.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  3. #3
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Change code to an input box to enter the file name

    Something like this.

    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  4. #4
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    Thank you both of you!!

    this has worked perfectly!

    Would it be possible to add an error message to the code so if the file is not open it tells you rather than creating an error to debug?

    "Bill of Materials Not Open Please Open First"

  5. #5
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    I have two separate codes and i'm not sure which bit i would put the code in,


    Please Login or Register  to view this content.

    then the macro to open the user form.

    Please Login or Register  to view this content.

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Change code to an input box to enter the file name

    This opens it for you if still closed.
    Replace "Filename" with wBook.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    bakerman2,

    Where should i put this code? Will it still work if my root folder is different every time?

  8. #8
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    I still get an error on the macro, i'm not sure if i have put it in the correct place.

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    This is what i tried in the macro, but again it came up with an error.

    Please Login or Register  to view this content.

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Change code to an input box to enter the file name

    Put the function in a standard module.
    In your userform module this.
    Also if your rootfolder is always changing you would have to substitute the InputBox to a FilePicker dialogue box.

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    Hi Bakerman2,

    I have put the function in a new module, and then inserted the code above, but i get an error saying \\NEWBENSON\Company\IMC Issue Sheets\wBook can not be found, it doesnt seem to be searching for the .xls file.

    any suggestions?

  12. #12
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    I have decide that the file location will be in the same place every time so no need to worry about that any more

  13. #13
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Change code to an input box to enter the file name

    I just copied your codelines so it didn't occur to me immediatly but wBook needs to be without the double quotes.

    Please Login or Register  to view this content.
    Last edited by bakerman2; 03-31-2017 at 04:06 AM.

  14. #14
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    I'm not sure what that means, very new to vba code sorry!

  15. #15
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Change code to an input box to enter the file name

    Just replace the same line in the code with the line in my previous post.

  16. #16
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    I have managed to work out how to get the .xls open and that now works perfectly!

    I have one other little problem now which i was unaware off til just now, Would it be possible to add another input box to get the worksheet name?

    Please Login or Register  to view this content.
    I have just tried to create the code myself but doesnt work thing i have something wrong!

  17. #17
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Change code to an input box to enter the file name

    Same as wBook, use wSheet without the double quotes.
    Last edited by bakerman2; 03-31-2017 at 05:36 AM.

  18. #18
    Registered User
    Join Date
    02-21-2017
    Location
    Castelford
    MS-Off Ver
    2013
    Posts
    89

    Re: Change code to an input box to enter the file name

    I thought i was close! just tried it and it worked perfectly!

    Thanks for your help through all this!

    REP ADDED

    Cheers
    ND

  19. #19
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Change code to an input box to enter the file name

    You're very welcome and thanks for rep+.
    If this answers your question please mark the 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. [SOLVED] VB Code to enter cell value when file is open
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-20-2016, 01:46 PM
  2. Change VBA code using input box
    By LLL0422 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-28-2014, 03:35 PM
  3. Formula to enter data/change the colour of a cell if a file isn't updated
    By alexander.small in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 10-31-2013, 09:58 AM
  4. Replies: 0
    Last Post: 10-02-2013, 01:47 AM
  5. [SOLVED] VBA Code to Enter File Name, Activate the File, and Run Macro on File
    By DHartwig35805 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-09-2012, 03:49 PM
  6. Doing away with an Input box - how to change this code?
    By HankMcSpank in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-13-2009, 09:22 AM
  7. How to enter automatic entry of file name in to VBA Code?
    By mvel_sky in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-28-2009, 03:43 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