UNSOLVED

Crosbylake

updated

18 years ago

C

Crosbylake

73 Posts

0

1849

January 30th, 2009 08:00

VB for Excel

I have two Excel Workbooks open A & B. Both have VB subroutines. I have A activated and am running Sub "a1" how can I from "a1" call and run Sub "b1" that resides in Workbook B? What I currently have to do is put a copy of "b1" in "a1".

Thanks for help,

Crosbylake

  • abach

    1728 Posts

    965

    1

    Posted January 31st, 2009 04:00

    The easiest way is to have both workbooks open, then use the following:

    Application.Run "'WorkbookWithSub.xls'!TheSubName"

    You could also use an add-in created from workbook A to be available in workbook B, or save workbook A's procedure as a project. If the above method does not work, let me know and I'll post info on the add-in or project.

  • Crosbylake

    73 Posts

    965

    1

    Posted January 31st, 2009 07:00

    Alan Testing the code as follows. What have I coded incorrectly?

     I have Workbooks Book1.xls and Book2.xls both open on the desktop

    Book1 has  a sub Test and Book2 has the sub Test2

    The following code gives an error when run in Sub Test in Book1.xls

    Application.Run "'Book2.xls'!Test2"

    Thanks for your help on this issue  and in the past.

    Crosbylake

  • abach

    1728 Posts

    965

    0

    Posted January 31st, 2009 15:00

    Make certain both workbooks are saved and in the same folder. If this does not work, post the error.

  • Crosbylake

    73 Posts

    965

    0

    Posted February 3rd, 2009 18:00

    Both workbooks are in same folder, but that didn't help. The error I get is 1004, but this is too general. The example given for the RUN method when the F1 key is pressed ---is returning an argument from a macro or function. I can see no difference (from your example) in the construct of the sample statements. The test subroutines used are below

    Sub Test()

    Workbooks("Book2.xls").Activate

    Application.Run "'Book2.xls'!Test2"
    End Sub

    Sub Test2()
    Sheets("Sheet1").Range("B4") = 7
    End Sub

    Crosbylake

  • abach

    1728 Posts

    965

    0

    Posted February 5th, 2009 06:00

    Are you sure the worksheet is not protected? This will give the error 1004.

  • Crosbylake

    73 Posts

    965

    0

    Posted February 5th, 2009 12:00

    Neither the Workbooks or the Worksheets are protected.

    When I use the F1 key on error message the example returned is a function or macro that returns an argument not a sub which I am using. Does that shed any light? I feel badly using your time on this issue. I can use a work around but your approach seems so much more powerful that I wanted to use it in other applications.

    Crosbylake

  • abach

    1728 Posts

    965

    0

    Posted February 5th, 2009 13:00

    Can you tell me in more detail what it is you're trying to accomplish? This code works on my Excel 2003 and 2007. Maybe by knowing what you're trying to do, I'll come up with a solution.

    If possible, could you attach the files to an email for me to look at?

    abach at absupportweb dot com