Consulting

Results 1 to 6 of 6

Thread: application.run - return value byRef

  1. #1
    VBAX Regular
    Joined
    Apr 2018
    Posts
    25
    Location

    application.run - return value byRef

    Hi,

    i am using event procedure that runs procedure from another wokbook.

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As range, Cancel As Boolean)
    
    
    
    
        Call Application.Run("workbookName.moduleName.someProcedure", Target, Cancel)
    
         ' cancel = false in this line, even though it is "byRef" and the target procedure changed it to false
    End Sub

    sub someProcedure(target as range, cancel as boolean)
    'bahbah
       cancel = true
    end sub

    how do i get the [cancel] value as set by the [some procedure] ?

    thanks

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Call Application.Run("workbookName.moduleName.someProcedure", Target, True)
    sub someProcedure(target as range, cancel as boolean)
    Dim MyCancel as boolean
    MyCancel = Cancel
    'blah, blah
    
    'Cancel = true
    
    MsgBox MyCancel
    end sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Although your called procedure defines the argument as the default byref, when you use Application.Run all arguments are passed as ByVal.

    If you want to pass it ByRef, you need to fool it. One way is to create a late bound application object and use that object in the run statement, like so

    Sub Testit()
    Dim Cancel As Boolean
    Dim app As Object
    
        Set app = Application
        app.Run "'myBook.xlsx'!someProcedure", Range("A1:A2"), Cancel
    
         ' now cancel = true in this line, as defined in the called procedure
        MsgBox Cancel
    End Sub
    BTW, I always prefer to define the argument types for clarity

    Public Sub someProcedure(ByRef target As Range, ByRef Cancel As Boolean)
    'bahbah
       Cancel = True
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Regular
    Joined
    Apr 2018
    Posts
    25
    Location
    Quote Originally Posted by xld View Post
    If you want to pass it ByRef, you need to fool it. One way is to create a late bound application object and use that object in the run statement, like so
    thank you good sir, fooling it with late binding works.
    My I ask, how dit you come out with this idea?

  5. #5
    VBAX Regular
    Joined
    Apr 2018
    Posts
    25
    Location
    Quote Originally Posted by SamT View Post
    Call Application.Run("workbookName.moduleName.someProcedure", Target, True)
    sub someProcedure(target as range, cancel as boolean)
    Dim MyCancel as boolean
    MyCancel = Cancel
    'blah, blah
    
    'Cancel = true
    
    MsgBox MyCancel
    end sub
    i am afraid you didn't really get the question

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by mg25683 View Post
    thank you good sir, fooling it with late binding works.
    My I ask, how dit you come out with this idea?
    I have to admit I didn't. I used to accomplish with a convoluted class trick, but I got this from an Excel mate a couple of years back, it is simple, and works a treat.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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