Can a function behave differently based on the calling method?

AndyTampa

Board Regular
Joined
Aug 14, 2011
Messages
199
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I'm working on this function to validate dates. I've written the function to be called from certain CommandButtons and from another function. I want this function to ask for a proper FTD if it was called from the CommandButtons and ask for a proper PTD if it was called from the MTBalPTD function.

Answers I've found are beyond my understanding. I've tried ChatGPT and it keeps giving answers that don't work. Then it keeps trying workarounds that get exponentially longer the more it tries.

I'm trying to use Application.Caller. I've used it in If statements. I've used it in Case Select statements. I'm getting Type Mismatch errors, Error 2023, and Run-time error '424' Object Required all depending on the different variations I've tried.

This is the last variation I tried before my eyes shut down for the night.

VBA Code:
    Dim callerName As String        ' What called this function?
    
    callerName = Application.Caller
    
    Select Case True
        Case callerName Like "*MTBalPTD*"
            promptType = "PTD"
        Case callerName Like "CommandButton*"
            promptType = "FTD"
        Case Else
            promptType = "date"
    End Select

This function already has so many variables, I was hoping to prevent adding one and if possible, getting rid of the one I added here. Can someone make this mud a little clearer?
 
Application.caller will only work if your function is called from a cell or from a button/shape where it was assigned as a macro.
So how can a function know that is was called from another function/sub/ActiveX button? That's what i was trying to do.
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I don't understand what you mean by a 'snag'. Can you post your entire Function ValidDate instead of just code snippets?
I resolved the snag. At the time, I wasn't sure if what I understood from your post was the cause of it. It wasn't.
 
Upvote 0
Via a parameter as previously suggested.
 
Upvote 0
Via a parameter as previously suggested.
Yeah, that works, but now I have to edit other buttons when what I really wanted to be able to do is specify variations in only the one function. The Application.Caller and Case commands looked very promising together. I'll keep it like this for awhile.
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top