+ Reply to Thread
Results 1 to 9 of 9

VBA Code to Enter File Name, Activate the File, and Run Macro on File

  1. #1
    Registered User
    Join Date
    01-09-2012
    Location
    Huntsville, Alabama
    MS-Off Ver
    Excel 2007
    Posts
    29

    Thumbs up VBA Code to Enter File Name, Activate the File, and Run Macro on File

    ALCON,

    First of all let me say hello, I am a new member and wanted to greet all my fellow Excellians.

    Now, I am having fits trying to get this to work. All I want is for a user to enter a file name in an "InputBox" and then take that entered file name and run a macro against it to change some static data on the worksheets in the file. There is not a consistent amount of worksheets across files, but each worksheet is identical in format.

    So, what I have now (that doesn't work by the way) follows:
    Please Login or Register  to view this content.
    As I said, what I have doesn't work. I can enter the file name fine, after that, crashes with run time error.

    Any and all assistance/advice/suggestions will be greatly appreciated and I would like to thank you all in advance for your understanding and cooperation in this matter.

    Sincerely,

    Confused Analyst
    Thank you to all who assisted, and you know whom you are, besides, I think everyone out here deserves Kudos for having to deal with an MS Office product on a daily basis. I just wish my co-workers would see that Excel can do more than add a column of numbers. Most of what I am doing, with ya'lls help, is so far above their heads that they will most likely never understand, but they are a smaaat group of people though, just not computer savvy as some. Since I come from a computer analyst/programmer background, transitioning into the Financial arena has been easier on me than most. But I am blessed with help all around, and especially from here. I have had more problems solved by searching here for solutions, or by posting a question. Thanks to all who replied, and a special hello to my Canadian responder. I grew up in Canada, not in Toronto, but in Ottawa, and I sure do miss it, especially the winters. Yes, I know I am crazy, but I love snow, the deeper the better. Here in the SE USA we get an inch of snow and they close down the entire city for a week or so. Just don't get that, all they have to do is forecast snow and the schools are immediately closed. Kind of makes me wish I had of grown up down here, but not that hard.....ha ha.

    Thanks again to all who assisted me.
    Last edited by DHartwig35805; 01-09-2012 at 03:45 PM. Reason: Solved by Member

  2. #2
    Forum Contributor
    Join Date
    08-14-2006
    Location
    USA
    MS-Off Ver
    2019
    Posts
    686

    Re: VBA Code to Enter File Name, Activate the File, and Run Macro on File

    your code assume the file the user enters is OPEN.

    change Windows('filenam1').Activate to
    Workbooks(filenam1).Activate

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: VBA Code to Enter File Name, Activate the File, and Run Macro on File

    Here you go

    Please Login or Register  to view this content.
    Please note that the code of the macro Change_Year_data should be placed in the same code page
    Regards,
    Khaled Elshaer
    www.BIMcentre.com

    Remember To Do the Following....
    1. Thank those who have helped you by clicking the Star below their post.
    2. Mark your post SOLVED if it has been answered satisfactorily:
    • Select Thread Tools (on top of your 1st post)
    • Select Mark this thread as Solved

  4. #4
    Registered User
    Join Date
    01-09-2012
    Location
    Huntsville, Alabama
    MS-Off Ver
    Excel 2007
    Posts
    29

    Re: VBA Code to Enter File Name, Activate the File, and Run Macro on File

    Quote Originally Posted by carsto View Post
    your code assume the file the user enters is OPEN.

    change Windows('filenam1').Activate to
    Workbooks(filenam1).Activate
    I tried the above, but received a Run-Time Error '9' "Subscript out of range", because it seems it is now treating the "filenam1" in parentheses as an array, and now I am really confused as to what to do. I just want to activate a closed file, make some cosmetic changes, then be done with it. I really do appreciate all of the advice so far, but still not quite there.
    Last edited by DHartwig35805; 01-09-2012 at 02:55 PM. Reason: Add more to my response

  5. #5
    Registered User
    Join Date
    01-09-2012
    Location
    Huntsville, Alabama
    MS-Off Ver
    Excel 2007
    Posts
    29

    Re: VBA Code to Enter File Name, Activate the File, and Run Macro on File

    Quote Originally Posted by Kelshaer View Post
    Here you go

    Please Login or Register  to view this content.
    Please note that the code of the macro Change_Year_data should be placed in the same code page
    Thank you for this block of code, but the key here is to get the user to ENTER the file name, then take that entered file name, and perform the conversion upon that. THis example doesn't ask the user to input any information at all. Thank you again though.

  6. #6
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: VBA Code to Enter File Name, Activate the File, and Run Macro on File

    Dhartwig35805,
    Did you try my code?

  7. #7
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: VBA Code to Enter File Name, Activate the File, and Run Macro on File

    Case 1: File not open and user will select it rather than typing it

    Please Login or Register  to view this content.
    Case 2: File not open and user will type the file name including the path

    Please Login or Register  to view this content.
    Case 3: File is already Opened and user will type the name of the file without the path

    Please Login or Register  to view this content.

  8. #8
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: VBA Code to Enter File Name, Activate the File, and Run Macro on File

    Dhartwig35805,
    Thanks for the good evaluation.
    Please mark this thread as SOLVED

  9. #9
    Registered User
    Join Date
    01-09-2012
    Location
    Huntsville, Alabama
    MS-Off Ver
    Excel 2007
    Posts
    29

    Re: VBA Code to Enter File Name, Activate the File, and Run Macro on File

    Yes, I did, and it worked as you said it would. It is probably the way i am going to go, with the user selecting a file, that way they don't have to write down the long file names, much better than what I had. TYVM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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