+ Reply to Thread
Results 1 to 11 of 11

Called or Directly excecuted routine

Hybrid View

  1. #1
    Registered User
    Join Date
    06-05-2009
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    50

    Called or Directly excecuted routine

    Hi Forum Friends
    I want to thank you all for the solutions/support I received in completing my (for me) difficult workbook.
    I still have a tricky problem. In the example below, can Multibeep "know" when it is being called from Beep versus being excuted directly? And if so can I direct a different sequence?
    Sub Beep_21()    
        Call MultiBeep(5)
    End Sub
    
    '----------------------------------------------
    Sub MultiBeep(numbeeps)
        For counter = 1 To numbeeps
            Beep
        Next counter
        If called by Beep then
            do A
        Else
            do B
        End if
    End Sub
    Of course in valid code.
    Many thanks
    Al
    Last edited by aljanga; 12-11-2009 at 12:39 PM.

  2. #2
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Called or Directly excecuted routine

    You could add an (optional) variable to enter when you call multibeep. I'd go with boolean, then branch if var = true ...
    else...
    Make sense?
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Called or Directly excecuted routine

    I'm still trying figure out how to directly activate a macro that has required initiation variables. I can't get it to activate, only when it is called from the other routine that is feeding in the variable.

    These don't work at all:
    Option Explicit
    
    Sub MultiBeep(numbeeps)
            MsgBox "Hi there"
    End Sub
    
    Sub MultiBeep(numbeeps As Long, Optional Remote As Boolean)
        For counter = 1 To numbeeps
            Beep
        Next counter
        If Remote Then
            MsgBox "called remotely"
        Else
            MsgBox "called directly"
        End If
    End Sub
    Last edited by JBeaucaire; 12-10-2009 at 04:13 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Called or Directly excecuted routine

    Here is what I have (based off yours JB). Seems to work fine.

    Public Sub multibeep(numbeeps As Long, Optional remote As Boolean)
    
        For i = 1 To numbeeps
            MsgBox i
        Next i
        
        If remote = True Then
            MsgBox "Called Remotely"
        Else
            MsgBox "Called Directly"
        End If
        
    End Sub
    
    
    Public Sub testdirect()
    
    multibeep 2
    
    End Sub
    
    Public Sub testremote()
    
    multibeep 2, True
    
    End Sub

  5. #5
    Registered User
    Join Date
    06-05-2009
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    50

    Re: Called or Directly excecuted routine

    Hi Forum Friends
    Many thanks for all your efforts. The "Two Step" method by Dave works
    fine, so I use that extra step to solve the problem.
    Al

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Called or Directly excecuted routine

    Glad you got what you needed, but as I stated originally and also is still true for Dave's method based off of mine, I cannot get the "MultiBeep" macro to initiate directly. I can only get it to run when another macro activates it while feeding in variables.

    Dave, how do you get it to activate without a second macro? I thought that was the whole point?

    The macro "testdirect" doesn't actually resolve the original request since it, too, is actually a remote activation.

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Called or Directly excecuted routine

    Seems an interesting thread - by directly do you mean from XL - ie via F8 ?

    If you do then you could use Module level variables (or Public if nec.), eg:

    Dim numbeeps As Long
    Public Sub multibeep()
        Dim i As Byte
        For i = 1 To IIf(numbeeps, numbeeps, 1)
            MsgBox i
        Next i
        If numbeeps Then
            MsgBox "Called Remotely"
        Else
            MsgBox "Called Directly"
        End If
        numbeeps = 0
    End Sub
    
    Public Sub testdirect()
    numbeeps = 2
    multibeep
    End Sub
    You could thus call multi beeps from XL via F8 or "indirectly" from another sub routine - assumption being for the latter that you would alter the local variable accordingly.

    Is that what you meant ?

    Sorry if I've misinterpreted

    (you could add Input dialog to trap requirements where beep = 0 if variable no. of beeps required)

  8. #8
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Called or Directly excecuted routine

    You are correct, I just suggested a work-around. You are correct though, it doesn't answer the request as specified. I did not read the initial request carefully enough.

    I think the work-around would be simpler, but you could create a global boolean variable that you could toggle to true before calling the macro from a different procedure. Like:

    Global Called As Boolean
    
    
    Public Sub multibeep(numbeeps As Long)
        For i = 1 To numbeeps
            MsgBox i
        Next i
        
        If Called = True Then
            MsgBox "Called Remotely"
        Else
            MsgBox "Called Directly"
        End If
        
    End Sub
    
    
    
    Public Sub testremote()
    
    Called = True
    multibeep 2
    
    End Sub
    This however still wouldn't work if you want numbeeps to be an argument. You could also make that a global variable, but then you are getting very complicated, and I think the work-around is much simpler.

    Global Called As Boolean
    Global numbeeps As Long
    
    Public Sub multibeep()
    
        If numbeeps = 0 Then
            numbeeps = Int(InputBox("How many beeps?", "?")) 'Or other source such as worksheet cell.
        End If
        
        For i = 1 To numbeeps
            MsgBox i
        Next i
        
        If Called = True Then
            MsgBox "Called Remotely"
        Else
            MsgBox "Called Directly"
        End If
        
    End Sub
    
    
    
    Public Sub testremote()
    
    numbeeps = 2
    Called = True
    multibeep
    numbeeps = 0
    Called = False
    
    End Sub
    Does that seem reasonable?

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Called or Directly excecuted routine

    Quote Originally Posted by davegugg View Post
    You are correct, I just suggested a work-around. You are correct though, it doesn't answer the request as specified. I did not read the initial request carefully enough.

    I think the work-around would be simpler, but you could create a global boolean variable that you could toggle to true before calling the macro from a different procedure. Like:

    Global Called As Boolean
    
    
    Public Sub multibeep(numbeeps As Long)
        For i = 1 To numbeeps
            MsgBox i
        Next i
        
        If Called = True Then
            MsgBox "Called Remotely"
        Else
            MsgBox "Called Directly"
        End If
        
    End Sub
    
    
    
    Public Sub testremote()
    
    Called = True
    multibeep 2
    
    End Sub
    This however still wouldn't work if you want numbeeps to be an argument. You could also make that a global variable, but then you are getting very complicated, and I think the work-around is much simpler.

    Global Called As Boolean
    Global numbeeps As Long
    
    Public Sub multibeep()
    
        If numbeeps = 0 Then
            numbeeps = Int(InputBox("How many beeps?", "?")) 'Or other source such as worksheet cell.
        End If
        
        For i = 1 To numbeeps
            MsgBox i
        Next i
        
        If Called = True Then
            MsgBox "Called Remotely"
        Else
            MsgBox "Called Directly"
        End If
        
    End Sub
    
    
    
    Public Sub testremote()
    
    numbeeps = 2
    Called = True
    multibeep
    numbeeps = 0
    Called = False
    
    End Sub
    Does that seem reasonable?
    Yes, the InPutBox thing is exactly what I came up with originally, but was stumped by trying to get the original macro to activate directly somehow and never finished that. Ah well, onward and upward.

  10. #10
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Called or Directly excecuted routine

    Using the Macro Dialog box, there is no way to pass a arguments to a Macro.
    Therefore, if a Macro has arguments it will not appear in the dialog box's list.
    Even if its arguments are optional.

    If a Macro has only optional arguments, it can be called from the Macro Dialog box by typing the name of the maco into the textbox (even though that name does not appear on the list.)

    Instead of introducing new arguments, you could make numBeeps an optional argument.
    If numBeeps equals its default value, then multiBeeps was called from the Macro dialog box (or was called by another macro that passed no argument).
    Otherwise, multBeep was called by some other macro, which did pass an argument.
    Sub MultiBeep(Optional numBeeps As Long)
        Dim counter As Long
        If numBeeps = 0 Then
            Rem called from Macro Dialog box
            numBeeps = 1
            'do B
        Else
            Rem argument passed when called by non-dialog box
            'do A
        End If
        For counter = 1 To numBeeps
            Beep
        Next counter
    End Sub
    Since your numBeeps is type Variant, an IsMissing test could do the same thing.
    Sub MultiBeep(Optional numBeeps)
        Dim counter As Long
        If IsMissing(numBeeps) Then
            Rem called from Macro Dialog box
            numBeeps = 1
            'do B
        Else
            Rem argument passed when called by non-dialog box
            'do A
        End If
        For counter = 1 To numBeeps
            Beep
        Next counter
    End Sub
    Last edited by mikerickson; 12-11-2009 at 06:03 PM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  11. #11
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Called or Directly excecuted routine

    Quote Originally Posted by mikerickson
    If a Macro has only optional arguments, it can be called from the Macro Dialog box by typing the name of the maco into the textbox (even though that name does not appear on the list.)
    I did not know this - thanks.

+ 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