Check Return value of private sub - excel

In an Excel 2007 workbook I have three Excel modules, each containing one subroutine. The Driver sub (UpdateDataFromOracle) calls the subs UpdateResponse and UpdateSched. The code is working fine, but I'd like to check the "return code" of each of the called subs. I only want the Driver sub visible to the user, so I made the subs in Modules 1 and 2 Private.
Module 1 Private Sub UpdateResponse
Module 2 Private Sub UpdateSched
Module 3 Public Sub UpdateDataFromOracle
Here's code from the Driver sub
Sub UpdateDataFromOracle()
'DECLARE VARIABLES
Dim varSchedReturn as variant
'...
Call UpdateResponse
Call UpdateSched
'I Would like to insert the "return code" check here
End Sub
Here's code from the Called sub
Option Explicit
Private Sub UpdateResponse()
'DECLARE VARIABLES
'...
If Sheets(strTempSheet).UsedRange.Rows.Count > 10 Then
UpdateResponse = 0
Else UpdateResponse = 90
End If
End Sub
To call the Private subs I had to abandon the "Call" and use"
Application.Run "Module1.UpdateResponse"
But I can't figure out how to get a return code that way.
I also made UpdateResponse and UpdateSched Private Functions, but I still couldn't figure out how to get a return code back.
When I made UpdateResponse and UpdateSched Public Functions, I can use a statement at the end of the called subs like:
Else UpdateResponse = 90
The problem is that the called subroutines are visible to the user if I leave the functions Public.
My goal is to have only the Driver sub visible to the user, and be able to evaluate some sort of "Return Code" from the called subs in the Driver sub.
Thanks for looking at this.

I didn't fully read the question, but change them to Function
Private Function UpdateResponse() As Integer
'DECLARE VARIABLES
'...
If Sheets(strTempSheet).UsedRange.Rows.Count > 10 Then
UpdateResponse = 0
Else
UpdateResponse = 90
End If
End Function
Then:
Dim response ' As Variant or Integer
response = Application.Run("Module1.UpdateResponse")
Also, there are 2 better ways with Option Private Module or a public variable in Module1
3 Ways to Call a Private Sub from Another Module

The answer #DougGlancy gave worked well. It's listed as a comment to my original question, so I'm adding this Answer to indicate that his answer was correct.

One option for Windows Excel that I have used is to set values on the user's machine that can be retrieved in a later process.
https://msdn.microsoft.com/en-us/library/z46c489x(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
Alternatives could be to set some properties of your Excel workbook or in Excel you can create names and values associated with them. (part of named ranges).
Lastly, you can add and change values in the windows registry.

Related

How to invoke ActiveX procedure outside of it's Worksheet scope?

I've got an extensive project.
Amongst other things, it contains an
UserForm
Worksheet (Data) with a button called import_button
Either way, I've got one procedure inside the UserForm vba code, which acts something akin to mine.
After it is executed, I wanted to Call the import_button_Click procedure, from the Data Worksheet, however obviously, the reference is unknown, given the import_button_Click procedure is Private inside the Data Worksheet module.
Is there perhaps a way, I could invoke the import_button_Click procedure outside of the Sheet1 (Data) module?
EDIT:
Should probably note, this is the structure of the import_button_Click procedure.
Public Sub import_button_Click()
Dim prva As Range: Set prva = Sheets("Zdrojove").Range("A1")
Call import_data(get_delimeter(prva, ";", ",", ".", "-", "/"), False)
Call validate_format
End Sub
I can't recreate the issue once changing the Sub to Public. Inside CommandButton1:
Inside Module1:
Running Test:
An option would be to replace the ActiveX button on the Worksheet by a Form Control button. This would allow you to get the exact procedure, now hosted in a Module instead of in the Worksheet... Then you can get the button to execute whatever macro (or sub) you want, while being able to call this procedure from other piece of code at the same time.
There're several ways to make it work:
You declare import_button_Click Public
Public Sub import_button_Click()
'Your code
End Sub
You create another Public Sub (inside Sheet1 (Data)) to call the Private one.
Private Sub import_button_Click()
'Your code
End Sub
'Use this outside the sheet
Public Sub public_import_button_Click()
Call import_button_Click
End Sub
Hope this helps.

Difference between Calling a Sub and Application.Run

In my business, we have a few teams that work on very simple macros. I'm trying to make them all readable to each other and in somewhat of a similar format, so new joiners can start working on the data.
I mention simple macros, because no one will be using Subs with arguments - most are derived from Macro Recorder anyway
Half of the teams use:
Sub button()
Call sub1()
Call sub2()
Call sub3()
Call sub4()
End Sub
And the other half use
Sub button()
Application.Run("sub1")
Application.Run("sub2")
Application.Run("sub3")
Application.Run("sub4")
End Sub
I understand that if your sub has no arguments, then Application.Run has a use - but being as there's barely any notation on it - is there a reason people still use Application.Run("")?
Can every use of it be beaten in speed and process by Call?
You can pass parameters through application.run as well. I use it when I am looping through macros. in your above example instead of having to write this:
Sub button()
Call sub1()
Call sub2()
Call sub3()
Call sub4()
End Sub
you could write this:
for i = 1 to 4
application.run("sub" & i)
next i
if the subs took in a str parameter you could do this:
for i = 1 to 4
application.run("sub" & i, strVariable)
next i
I use Application.Run if I’m trying to run a sub that is private in another module. If I have a some kind of template where I want to hide the macros from the users I will declare it as private so they can’t run/see the macro from there macros dialog box.
In module1 I have
Private Sub priv()
MsgBox “Private”
End Suv
In module2 the below will give you a Sub or Function not defined error.
Sub callPriv()
Call priv()
End Sub
But in module2 this will run and display the message box
Sub callPriv()
Application.Run “priv”
End Sub
It’s also useful to use Application.Run if you are calling a sub in your sheet or thisWorkbook modules.
I am posting some of this as an answer, because I cannot comment.
# Badja : You said ….” I understand that if your sub has no arguments, then Application.Run has a use – …” I am not sure if you are possibly implying that Application Run can have no arguments? - That is not the case. In principal , you can use arguments with Application.Run just as you can with Call. The syntax and general working of it can be a bit more difficult to understand than with Call. I agree with you , that documentation on Application.Run is very sparse.
#Hasib_Ibradzic : I was not aware that you can use the Call function with strings. ??
As far as I know, it is one of the advantages of Application.Run over using Call, that the macro name part of the argument is taken as a sting, so that you can build that string up with variables. So you are not limited to hard coding, as I think is the case with using Call
#Dude_Scott : In your example, I think it is advisable to suggest/ note that including the module code name could be advisable. Without this, you could experience problems if you had Sub routines with the same name in different modules.
In your example, this could be advisable to do
Sub callPriv()
Application.Run "Module1.priv"
End Sub
_._____________
Some other things that might be useful to Note:
If you have a macro in another module which is Public, then you can Call it from another module
For example. In my ( German ) Excel I have in my first normal module , code name Modul1 , ( In English Excel, I think that would typically be by default Module1 ), this
Public Sub PbicModule1() '
MsgBox "Pubic Modul1"
End Sub
And in my first worksheet class code module, code name Tabelle1 ( In English Excel I think that would typically be by default Sheet1 ) , I have this
Public Sub PbicTabelle1() '
MsgBox "Pubic Tabelle1"
End Sub
And in my ThisWorkbook class code , code name DieseArbeitsmappe ( In English Excel , I think that would typically be by default ThisWorkbook ), I have this
Public Sub PbicThisWorkbook() '
MsgBox "Pubic ThisWorkbook"
End Sub
The following macro will work when it is in any module
Private Sub CallMePubics() ' https://stackoverflow.com/questions/55266228/difference-between-calling-a-sub-and-application-run
Call Modul1.PbicModule1
Call Tabelle1.PbicTabelle1
Call DieseArbeitsmappe.PbicThisWorkbook
End Sub
That last macro would fail if the called macros were Private.
This following macro equivalent using Application.Run , would work regardless of whether the called macros were Public or Private
Private Sub AppRunMePubics()
Application.Run Macro:="Modul1.PbicModule1"
Application.Run Macro:="Tabelle1.PbicTabelle1"
Application.Run Macro:="DieseArbeitsmappe.PbicThisWorkbook"
End Sub
So , the conclusion from that is adding slightly to what Dude_Scott said:
Application.Run allows you to run, from anywhere, both Private subs and Public subs that are anywhere.
Call only allows you to run from anywhere, Public subs that are anywhere
Ref:
https://stackoverflow.com/questions/31439866/multiple-variable-arguments-to-application-ontime
http://excelmatters.com/2017/04/07/passing-arguments-byref-using-run/
Example Workbook with my coding: MainFile.xls : https://app.box.com/s/prqhroiqcb0qccewz5si0h5kslsw5i5h
http://www.tushar-mehta.com/publish_train/xl_vba_cases/1022_ByRef_Argument_with_the_Application_Run_method.shtml

"Include" in Excel VBA?

I have a user-form which is made up of many subs, this is assigned as a macro to a button on the worksheet. When the user is finished with this user-form they can press a button on it which causes its visibility to become false and when entered again everything appears how it was left resulting in a save like feature.
I now need to apply this to multiple buttons on the worksheet and each user form needs to have the exact same code and same buttons but be a separate form as each individual button requires it's own save like feature. The way I was planning on doing this was to copy the existing user form and paste it many times with different names however, if a modification is required it will take a long time to carry out therefore, is there a method such as "include" which could use a base module from which all the code is accessed so that if I ever need to change anything I just do it on that one module and everything else updates via the include?
EDIT:
I now have a public function called costing() and am getting an error when I used:
Private Sub material_Change()
Call costing
End Sub
You can have multiple instances of the same form. You can use this to retain multiple sets of form values
Try this:
Create your form, as usual. Let's call it MyForm
Create several buttons on your sheet. My example uses ActiveX buttons, but Form Control buttons can be used too. Let's call them CommandButton1 and CommandButton2
In your form module, include a Terminate Sub, which includes this code
Private Sub UserForm_Terminate()
' any other code you may need...
Unload Me
End Sub
The Form buton to save/Hide the form needs to be
Private Sub btnSaveAndHide_Click()
Me.Hide
End Sub
The Sheet Button code is as follows
The code is identical for each button (and calls a common Sub), and each button has its own Static form variable.)
The Error handler is needed to deal with the case a form is not properly closed. In this case the instance no longer exists, but the local Static variable is also not Nothing
Example shows form shown as Modeless, you can change this to Modal if you want.
Private Sub CommandButton1_Click()
Static frm As MyForm
ShowMyForm frm
End Sub
Private Sub CommandButton2_Click()
Static frm As MyForm
ShowMyForm frm
End Sub
Private Sub ShowMyForm(frm As MyForm)
If frm Is Nothing Then Set frm = New MyForm
On Error GoTo EH
frm.Show vbModeless
Exit Sub
EH:
If Err.Number = -2147418105 Then
On Error GoTo 0
Set frm = Nothing
Set frm = New MyForm
frm.Show
End If
On Error GoTo 0
End Sub
End result: multiple copies of the same form, each with their own values
In responce to comment How would I access the variables inside of each user form externally
In the example above the Form instances are only accessable in the Command Button Click Handler routines, or within the Form module itself. If you can write your code in the form module, then no change is needed.
To make the Form instances available elsewhere, consider moving their declaration to Module Scope of a standard Module. You could declare them as, eg individual variables, an array (either static or dynamic), a Collection, a Dictionary. Which structure is best will depend on how you want to manage and access your form instances.
For example, a Static Array: Code in a standard Module
Option Explicit
Global MyForms(1 To 2) As MyForm
Update the CommandButton code to
Private Sub CommandButton1_Click()
ShowMyForm Module1.MyForms(1)
End Sub
Private Sub CommandButton2_Click()
ShowMyForm Module1.MyForms(2)
End Sub
Private Sub ShowMyForm(frm As MyForm) no change, same as before
The code works the same as before, but you can now access the Global variable in a standard Module
Sub Demo()
Dim i As Long
For i = LBound(MyForms) To UBound(MyForms)
If Not MyForms(i) Is Nothing Then
MsgBox "Form " & i & " Value = " & MyForms(i).TextBox1.Value
End If
Next
End Sub
You don't need an "Include" (none exists in VBA); all you need to do is create a module and make the common methods public.
For example, if you create a module and have a function like this:
Public Function Add(first As Integer, second As Integer) As Integer
Add = first + second
End Function
Then you can access it like this from another module/form/class module:
Sub test()
MsgBox Add(3, 6)
End Sub

Calling a userform and returning a value

I have a vba code thats Auto_Open. It does some checks then prompts a userform that asks for username and password. I called this userform with userform_name.show.
My issue is how can I return a Boolean to my Auto_Open sub from the userform code.
I linked the code that verifies if the credentials are correct to the "Login" button on the form. this is the code that produces the Boolean. I need to return it to the Auto_Open.
Private Sub loginbutton()
Dim bool As Boolean
Dim lrup
Dim r As Long
Dim pass As String
loginbox.Hide
'are fields empty
Do While True
If unBox.Text = "" Or pwBox.Text = "" Then
MsgBox ("You must enter a Username and Password")
Else
Exit Do
End If
loginbox.Show
Exit Sub
Loop
'find pw reated to username (if existant)
lrup = UserPass.Range("A1").Offset(UserPass.Rows.Count - 1, 0).End(xlUp).Row
If unBox = "b0541476" And pwBox = "theone" Then
bool = True
Else
MsgBox ("Invalid username or password. Please try again.")
loginbox.Show
Exit Sub
End If
For r = 2 To lrup
If unBox = Cells(r, 1) Then
pass = Cells(r, 2).Value
Exit For
End If
Next
If pass = "" Then
MsgBox ("Invalid username or password. Please try again.")
loginbox.Show
Exit Sub
Else
bool = True
End If
End Sub
You can manage to do this without the use of public variables.
There appears to be a difference between show/hide and load/unload.
If you hide a form while it's still loaded it won't be cleared out, so you can reference the state of the controls on the form.
For example I was using a date picker (called DTPicker1) on a form, my code in the module looks something like this:
Dim NewDay As Date
Load FrmDayPicker
FrmDayPicker.Show
NewDay = FrmDayPicker.DTPicker1.Value
Unload FrmDayPicker
Debug.Print NewDay
On your form you can just use Me.Hide insteaded of Unload Me and this should work
Remove Dim bool As Boolean from the userform code area and declare it in the module as shown below
This is how your Code in the module would look like
Public bool As Boolean
Sub Auto_Open()
'
'~~> Rest of the code
'
UserForm1.Show
If bool = True Then
'~~> Do Something
Else
'~~> Do Something
End If
'
'~~> Rest of the code
'
End Sub
How about using a function instead of a sub?
Function loginbutton()
' your code
loginbutton = bool
End Function
Now in your calling code you can test for true/false
if loginbutton() then
'true responce
else
'false responce
end if
Update:
I was to quick to dismiss public variables. While both methods can work, Pub Vars and directly accessing items, sometimes it's not ideal to access an item directly if say it's a list.
I now have modules specifically for calling UserForms which only declar the public variables and call the userform. I can then call these modules from UserForms or Modules and have access to the public variable after the userform is closed.
Eg: Here is a module I use now, very basic, and all my other needs can just call this module/sub.
Public ColSelectorDic As Object
Public Sub Col_Picker_Sub()
Col_Picker_UserForm.Show
End Sub
It's simplest IMO to use Public Variables declared in the Module calling the UserForm. But, this has the caveat if you wanted to call this userform from separate modules, you will get errors regarding duplicate declarations/ambiguous names.
So, if you know it's only going to be called be the one module, Pub Vars all the way. In my case I was using a "Column Picker" userform, which was very simple and I wanted to be able to utilize it again in unforseen future projects so I attempted to resolve the above caveat.
See this answer for Public Variables, no need to repeat information --> https://stackoverflow.com/a/18966341/5079799
And this answer related to Accessing the Form Variables directly -->
https://stackoverflow.com/a/47919465/5079799 but I felt it could use some expanding.
Also, here is a good article which goes deeper in depth about accessing userform variables directly --> https://gregmaxey.com/word_tip_pages/userform_pass_data.html
So my UserForm looks like this and is named ColPicker:
Private Sub UserForm_Initialize()
Dim i As Long
lCol = Get_lCol(ActiveSheet)
For i = 1 To lCol
ColumnLetter = Col_Letter(i)
Me.ComboBox1.AddItem ColumnLetter
Next
End Sub
Private Sub CommandButton1_Click()
Me.Hide
End Sub
Sub PassVarFromUserForm()
ColPicker.Show
Dim ColLetter As String
ColLetter = ColPicker.ComboBox1.Value
Unload ColPicker
Debug.Print ColLetter
End Sub
Notice how the "Run"/Command Button in the UserForm just hides the form, I then store the values in a variable, THEN unload the form, from the module, via utilizing it's name. (You can only use unload me from within the userform).
The variable is then available inside module and can be declared in the beginning as public, or inside module, it doesn't matter as it can be declared differently in each module, the userform has no idea/reference to what the variable name the information will be stored in.

User form can't pass value to module

I've inserted a user form into a project that already contains a bunch of modules. By playing around with the code in the user form, I've verified that I can return the value from a combo box.
User form code:
Public SelectedPacking As Integer
Private Sub CancelButton_Click()
UserForm1.Hide
End Sub
Private Sub OKButton_Click()
SelectedPacking = ComboBox1.ListIndex
Call DemoDialogOk
'Return list index value to cell C50
Worksheets("Inputs & Results").Range("C50") = SelectedPacking
Unload UserForm1
End Sub
My problem is that I can't pass this value on to any of the macros written in the modules.
Module code:
Public Sub ShowComboBox()
UserForm1.Show
End Sub
Public Sub DemoDialogOk()
ival = SelectedPacking
'Return value of ival (list index value from combo box) to cell C17
Worksheets("Packed bed (Random)").Range("C17") = ival
End Sub
Obviously the module contains more useful code, but I've commented out everything to try and figure out where I'm going wrong. I've been changing some things around, but I still can't get anything to appear in cell C17, so I think I'm missing something fundamental.
I think two options: 1) change DemoDialogueOK to accept variables:
Public Sub DemoDialogOk(SelPack as integer)
' ival = SelectedPacking
Worksheets("Packed bed (Random)").Range("C17") = SelPack
End Sub
Private Sub OKButton_Click()
SelectedPacking = ComboBox1.ListIndex
Call DemoDialogOk(SelectedPacking)
...
End Sub
Or option two: fully qualify the variable from the useform i.e:
Public Sub DemoDialogOk()
ival = ufYourForm.SelectedPacking
...
End Sub
Public variables in userforms don't appear to be as "public" as module level...
Tipping on top of Simon's answer, you could pass the entire userform if you'd like. This would give you access to all the pieces of it and is especially useful if you need to do some validation on, say, different checkboxes being checked or not.
Sub inOurUserForm()
Call inADifferentModule(Me) 'Passes this userform
End Sub
Sub inADifferentModule(ourForm As UserForm1)
'Passed the form, and using it like a class (As whatever the form is called)
If ourForm.chkYes = True Then
'Do something
Else
'Do something else, like
ourForm.chkYes = False 'Because we are passing the object itself _
rather than a copy, at least in my understanding
End If
End Sub
And you don't necessarily need to pass the userform, as you could just reference it as an object itselft e.g.
UserForm1.chkYes
A very easy solution to this would be to declare a variable within the Userform (UserForm1 in this example)
Public Pass as string
This Pass would contain the string where you store the password. Once you store the password, you can hide the form
Me.Hide
Within the module, you can open the Form as modal
UserForm1.Show vbModal
Now after all the code inside the userform is run, the password can be retrieved within the module -
UserForm1.Pass
You can then unload the hidden form
unload UserForm1

Resources