I am trying to write a sub in VBA that looks like the following,
Public Sub Value(X As Double)
Code in here...
End Sub
However, whenever I try to run this, it doesn't recognize the Sub I just wrote. Why is this?
I can get the sub to work if I write it like,
Public Sub Value()
Dim X As Double
Code in here...
End Sub
However, I need to do it the first way. Is there something that I'm missing, that I need to include in my code to be able to write it the first way?
See this example:
Public Sub Value(X As Double)
MsgBox X + 1
End Sub
Public Sub Test()
Value 2.2
End Sub
Running Test will give you:
Related
I have trying to call a module by assigning my sub name however it does not work
I tried by
Private Sub Filtert04_click()
Call filter 'the sub name
End Sub
It would be great if someone could help thx
Make sure the Sub in the module is defined as Public
Public Sub DoIt()
MsgBox "Hi"
End Sub
and then from your userform, you can call it like this:
Private Sub Filtert04_click()
DoIt
End Sub
I'm trying to write a macro that will have many different buttons. I would like all the buttons to trigger the same function but I need to set a variable in the function to identify which button triggered the macro.
I'm not really sure how I should do this. Is there a way I can say like variableA = ClickedButton()?
Any advice is appreciated
Hi , I think something like this should work
Private Sub CommandButton1_Click()
myfunction "b1"
End Sub
Private Sub CommandButton2_Click()
myfunction "b2"
End Sub
Private Sub CommandButton3_Click()
myfunction "b3"
End Sub
sub myfunction (v as variant)
msgbox v
end sub
Try the following
In the button module:
Sub CommandButton1_Click()
myFunction btn_name:= Me.Name
End Sub
In a normal module:
Public Function myFunction(btn_name As String)
MsgBox "The button pressed was:" & btn_name
'do stuff
End Sub
Doing this, you pass the name of the pressed button (Me.Name) to the function as a variable, so it can be used generally inside that function.
I am trying to delete a specific column from a specific worksheet and I cannot figure out why this sub is giving me this error:
Compile error: Sub or Function not defined
I have a different private sub that i have set up and call the same way and works fine, so I am thinking my syntax is off? (I'm level zero on vba 😊)
Calling the Sub
Sub HideColumns()
...
Call Del_Col_A
...
End Sub
The Sub
Private Sub Del_Col_A()
Sheet("Sheet7").Columns(1).EntireColumn.Delete
End Sub
You have forgotten the 's' on the end of 'sheets'
Private Sub Del_Col_A()
Sheets("Sheet7").Columns(1).EntireColumn.Delete
End Sub
Make sure "Sheet7" is actually your sheet name, so:
Sheets("your sheet name").Columns(1).EntireColumn.Delete
if its not "Sheet7"
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.
So I'm using excel 2010, and right now, I'm trying to use calculate the value of a variable in one sub, and want it to be used in multiple subs, except I can't figure out how to do this. Here is an example of what I'm using in its most basic form
Public Sub Calc()
Dim i As Integer
i = 10 - 5
End Sub
'Other sub that will use the value for i calculated in the Calc sub
Sub Macro()
y = i +5
End Sub
How am I suppose to be able to use/pass this value for "i" in/into the sub macro?
Move the Dim above the subs:
Public i As Integer
Public Sub Calc()
i = 10 - 5
End Sub
Sub Macro()
y = i + 5
MsgBox y
End Sub
Sub MAIN()
Call Calc
Call Macro
End Sub
Try running MAIN()
Insert new module and define i as:
Public i As Integer
Then you'll be able to use it whenever you want.
For further information, please see: Scope of variables in Visual Basic for Applications
I would like to use Function instead of Sub.
Sub Main()
Msgbox Macro(calc)
end Sub
Function Calc() as Integer
Dim i As Integer
i = 10 - 5
Calc = i
End Function
Function Macro(i as Integer) as Integer
Macro = i + 5
End Function