+ Reply to Thread
Results 1 to 18 of 18

vba excel to open files and to run a built in macro

  1. #1
    Registered User
    Join Date
    02-18-2015
    Location
    New York
    MS-Off Ver
    office2013
    Posts
    32

    vba excel to open files and to run a built in macro

    hello,

    I'm trying to create a way (maybe a vba code) or something a HTML application (HTA) that would do the following:

    in a folder, i have multiple excel files probably about 40-50 excel files.
    i want to create a way (vba code or HTML applicator HTA) that would open all of the excel files in this folder and run a macro automatically (Ctrl+G) for each of every file.
    after that, save and close all excel files.

    this is only a brief description of what I want to do, if needed more detail let me know.

    hoping someone would be able to help.

  2. #2
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: vba excel to open files and to run a built in macro

    A vba code perhaps something like this?

    Please Login or Register  to view this content.
    Alf

  3. #3
    Registered User
    Join Date
    02-18-2015
    Location
    New York
    MS-Off Ver
    office2013
    Posts
    32

    Re: vba excel to open files and to run a built in macro

    but how exactly to run this vba code?
    open a new excel?

  4. #4
    Registered User
    Join Date
    02-18-2015
    Location
    New York
    MS-Off Ver
    office2013
    Posts
    32

    Post Re: vba excel to open files and to run a built in macro

    i tried but it doesn't work
    here is the scripts, i copied the macro where you said to put my macro here. is this right?
    when i run the script, it ran to an error Line 3

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Last edited by ctiger; 12-16-2017 at 11:33 AM. Reason: Please use code tags!

  5. #5
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: vba excel to open files and to run a built in macro

    You say that there are a number of excel files but the folder name is "\csv\" and I do wonder are you opening csv files?

    I think the best you could do at the moment is to record a macro where you select a file from that folder and then post this macro.

    Alf

  6. #6
    Registered User
    Join Date
    02-18-2015
    Location
    New York
    MS-Off Ver
    office2013
    Posts
    32

    Re: vba excel to open files and to run a built in macro

    no, it's not a CSV files, it's just a folder name.

  7. #7
    Registered User
    Join Date
    02-18-2015
    Location
    New York
    MS-Off Ver
    office2013
    Posts
    32

    Re: vba excel to open files and to run a built in macro

    attach is the file i save with the vba code
    do i need to create this file actually ?
    Attached Files Attached Files

  8. #8
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: vba excel to open files and to run a built in macro

    Hello ctiger,

    Please use code tags when posting code!
    HTH
    Regards, Jeff

  9. #9
    Registered User
    Join Date
    02-18-2015
    Location
    New York
    MS-Off Ver
    office2013
    Posts
    32

    Re: vba excel to open files and to run a built in macro

    hi jeff,

    i edited and use code tags. sorry and thank you for the reminder.

  10. #10
    Registered User
    Join Date
    02-18-2015
    Location
    New York
    MS-Off Ver
    office2013
    Posts
    32

    Re: vba excel to open files and to run a built in macro

    hello alf,

    are you able to check my file?
    it's not working.

  11. #11
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: vba excel to open files and to run a built in macro

    Sorry for the delay. Xmas have taken 1st priority according to my wife but now I'm back with a bit of time for excel.

    The first part of the macro just opens the excel file, but I assume that the opened file does not contain any macro if so then you just run the commands
    after the line
    Please Login or Register  to view this content.
    so just add the lines from the first macro
    Please Login or Register  to view this content.
    Since you are adding new variables they must also be dim'ed either at the start of the macro or as in this case before the new macro runs.
    So you just adds the other macros after each other and after the last lines you need to include the loop so that all files are save and closed after modefication.

    You modified macro in post #4 is a bit wrong, it should look like this:

    Please Login or Register  to view this content.
    So you open the file do the modification, saves the file and closes it, then you open the next file .....

    As I said this assumes that the files you opens don't contains the macros you wish to run in order to modify them.

  12. #12
    Registered User
    Join Date
    02-18-2015
    Location
    New York
    MS-Off Ver
    office2013
    Posts
    32

    Re: vba excel to open files and to run a built in macro

    thanks for taking your time during the holiday to answer me.
    i put your new code to create a new excel file to run the new code macro. as attached file.
    nothing happen though, did i do anything wrong?
    Attached Files Attached Files

  13. #13
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: vba excel to open files and to run a built in macro

    First of all make a backup of you excel files before you run the macro in case something goes wrong so you don't mess up your data.

    Since I did not study you macro setup properly I missed how things works together i.e. from the main macro you call 4 sub macros in order to organize the data in the files. So you did not do anything wrong, it was my faulty understanding of the problem that messed things up. Sorry about that.

    After the first macro has done it's job by transferring / changing data in the opened file macro jumps back to main macro and execute next line that calls the
    second macro who modifies data and macro jumps back to main macro and third sub macro is called and so forth

    The problem (my bad as I did not realize how these macros were called from the main macro) is found in the end of the last macro
    "Sub AddCartonLabels(ws As Worksheet)"
    Change the lines:

    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    As the extra lines are found in the "Sub ModFiles" macro and when all lines in macro 4 is processed macro jumps back to "Sub ModFiles" and then do the

    Please Login or Register  to view this content.
    before it opens the next file in the folder.

    Well I must say I did learn quite a bit from your problem and interesting to see how one can split a long macro into several small stand alone macros.

    Alf

  14. #14
    Registered User
    Join Date
    02-18-2015
    Location
    New York
    MS-Off Ver
    office2013
    Posts
    32

    Re: vba excel to open files and to run a built in macro

    i remodeled the macro but still has:

    run time erro '9':
    subsciption out of range

    i'm attaching the new macro file and also my excel file that i'm working to convert
    Attached Files Attached Files

  15. #15
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: vba excel to open files and to run a built in macro

    Changed macro a bit, did a run letting the macro modify the uploaded usa-20-xxxxx file. As I'm uploading both files you can see if
    the modified usa-20-xxxx file has the layout as you expect.

    Alf
    Attached Files Attached Files

  16. #16
    Registered User
    Join Date
    02-18-2015
    Location
    New York
    MS-Off Ver
    office2013
    Posts
    32

    Re: vba excel to open files and to run a built in macro

    yes, exactly.
    however; when i downloaded it's not running in my computer
    how did you run it?

    it encountered a run time error:

    it has a run-time error "91"
    Object variable or with block variable not set


    pointing to: bk.Close SaveChanges:=True
    Last edited by ctiger; 12-19-2017 at 05:29 PM. Reason: solved

  17. #17
    Registered User
    Join Date
    02-18-2015
    Location
    New York
    MS-Off Ver
    office2013
    Posts
    32

    Re: vba excel to open files and to run a built in macro

    it works.

    thank you so much for taking your time to help me again and again. you are really Amazing !

  18. #18
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: vba excel to open files and to run a built in macro

    As you seems to get the macro to run I've deleted my original comments (thought there was a version problem Windows versus Mac)

    You are welcome and thanks for feedback.

    As this seems to solve you problem don't forget to mark thread "Solved" as per forum rules

    Alf

    Ps
    To mark your thread solved do the following:

    -Go to the top of the first post
    -Select Thread Tools
    -Select Mark thread as Solved
    Last edited by Alf; 12-19-2017 at 05:37 PM.

+ 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. macro to open files and copy specific sheet into new workbook (have open files part)
    By dangerdavedsp in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-04-2015, 02:23 PM
  2. [SOLVED] Save/Closed only macro related files and leave unrelated files open in excel?
    By rdowney79 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-12-2013, 04:17 PM
  3. [SOLVED] Excel 2010 VB macro to open all files in folder and run a simple macro
    By MSFLNA in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-08-2013, 02:54 PM
  4. Replies: 4
    Last Post: 08-17-2013, 07:17 PM
  5. Excel 2007 : Excel does not open files - only macro files
    By freeskier19 in forum Excel General
    Replies: 0
    Last Post: 02-02-2012, 07:42 PM
  6. Excel macro to open files
    By benjyd in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-16-2011, 12:23 PM
  7. Simple macro help-built a marco to open a daily text file
    By lars in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-05-2005, 02:06 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