Results 1 to 7 of 7

Thread: [RESOLVED] Run Excel Module code from Access...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2004
    Location
    Surrey, UK
    Posts
    163

    [RESOLVED] Run Excel Module code from Access...

    I am opening an excel spreadsheet from Access called test.xls like below. I have a Sub saved in the spreadsheet in Module1 called Test.

    How do I run that from Access???



    VB Code:
    1. Dim xlApp As Excel.Application
    2. Dim xlBook As Excel.Workbook
    3. Dim xlWSheet As Excel.Worksheet
    4.  
    5. Set xlApp = New Excel.Application
    6. xlApp.Workbooks.Open ("C:\Test.xls")
    7. xlApp.Visible = True
    8. Set xlBook = xlApp.Workbooks(1)
    9. Set xlWSheet = xlApp.Worksheets(1)
    10.  
    11. 'call xlapp.module1.test   ????????
    12.  
    13.  
    14. xlBook.Module1.test
    Last edited by strobinson1; Oct 25th, 2005 at 03:28 AM.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Sep 2004
    Location
    Surrey, UK
    Posts
    163

    Re: Run Excel Module code from Access...

    ok,

    sorted that one with

    Call xlApp.Run("mdltest.test")

    Any ideas on how to update Excel variables from Access??

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Run Excel Module code from Access...

    The variables are running in separate processes, and therefore cannot be updated directly.

    I would recommend making subs in Excel which can update them for you, eg:
    VB Code:
    1. Public Sub UpdateVar_Fred (NewValue as Integer)
    2.   Fred = NewValue
    3. End Sub
    ..hopefully you can call it like this:


    Call xlApp.Run("mdltest.UpdateVar_Fred 4")

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2004
    Location
    Surrey, UK
    Posts
    163

    Re: Run Excel Module code from Access...

    I've actually done it by copying my Access variable data to a cell on the spreadsheet and then in the excel code updating the excel variable by looking at that cell, and finally emptying the cell.

    Will try your method now.

    Is there a way to send data the other way aswell?? ie the Err value should the process fail in excel, i want to store the Err value in Access.....???

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2004
    Location
    Surrey, UK
    Posts
    163

    Re: Run Excel Module code from Access...

    Used syntax like this in Access to send Access Variable to Excel Function:

    VB Code:
    1. xlApp.Run "test", strName


    Still need to work out how to return a value back to Access from Excel....?????
    Last edited by strobinson1; Oct 24th, 2005 at 10:57 AM.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Run Excel Module code from Access...

    I would assume that Run itself is a function, and returns the value from the function that you call. If that is true, the following should work:

    myVar = xlApp.Run ("test", strName)


    Note that if the parameters (like strName above) are defined as ByRef you may be able to alter them in the function/sub and then read them on return to the caller program.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2004
    Location
    Surrey, UK
    Posts
    163

    Re: Run Excel Module code from Access...

    Yup...that's exactly how i did it!!

    Cheers!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width