VBA using RandBetween [duplicate] - vba

This question already has an answer here:
VBA - Sub or Function not Defined
(1 answer)
Closed 2 years ago.
Why I am getting an compilation error: "Sub or Function no defined" ?
Public Function generateCode() As String
generateCode = RandBetween(3, 12)
End Function

RandBetween is not a build-in function in VBA, it is an Excel function. As long as you are programming for Excel, you can access Excel functions via WorksheetFunction, in your case Application.WorksheetFunction.RandBetween(3, 12)

Related

Why is Visio VBA Exponentiation not working? [duplicate]

This question already has answers here:
VBA power operator (^) not working as expected in 64-bit VBA [duplicate]
(2 answers)
Closed 2 years ago.
Today Visio VBA is behaving differently. debug.print 2^2 prints "2 2" and debug.print 5.5^2 gives an error message "Compile error: Expected expression" while debug.print (5.5)^2 gives 30.25.
I don't remember ever seeing this before in the past probably 15 years of VBA programming, I expected to get 4 and 30.25
Use 2 ^ 2. The space is important.

VBA compile error with very simple sub macros with Expected: = [duplicate]

This question already has answers here:
VBA does not accept my method calling and gives Compile error: Syntax error [duplicate]
(2 answers)
Closed 3 years ago.
I have a small VBA macro in Word that will not compiles with an Expected: = error. Since these are all subroutines I am not sure what would cause this. I have simplified this as much as possible to the code listed below.
Sub test()
tmp = MsgBox("test")
test2("tmp2","tmp3") ' this is the line where the compile error appears.
End Sub
Sub test2(test1String As String, test2String As String)
MsgBox (test1String)
End Sub
I would not expect there to be any assignment errors with something this simple. Enter the code and try to run the test macro.
You are calling a Sub, not a Function. The syntax
test2("tmp2","tmp3")
calls the sub test2 with only one argument, namely an (invalid) expression ("tmp2","tmp3")
You should call it as
test2 "tmp2","tmp3"
The braces ( and ) turns the argument list in an expression.
Note: also when calling a function with braces around the argument list requires that the function result is assigned, otherwise the function arguments too are evaluated as an expresion
i = myFunction(1, 2) ' valid
myFunction 1, 2 ' valid
myFunction(1, 2) ' invalid
mySub(1, 2) ' invalid
mySub 1, 2 ' valid
mySub(1 + 2), (3 + 4) ' valid!
The last example calls the sub with two arguments, which both are expressions.

Creating parallel multiple tasks with variable parameter [duplicate]

This question already has answers here:
Is there a reason for C#'s reuse of the variable in a foreach?
(4 answers)
Starting Tasks In foreach Loop Uses Value of Last Item [duplicate]
(2 answers)
Closed 5 years ago.
This is a cumbersome behaviour when creating several task in a for loop:
Private Sub DoSomeTasks()
For i = 1 to 3
Dim oTest as cTest = New cTest
Dim oTask As Task = Task.Factory.StartNew(Sub() oTest.DoSimulation(i))
Next
End Sub
This code will create all tasks with the same i value (= 3). Anyone has experienced something similar? Anyone has a clue on how to solve it?

Excel VBA: How to use objects defined in a previous module in a new module without defining them again [duplicate]

This question already has answers here:
How to make Excel VBA variables available to multiple macros?
(3 answers)
Closed 5 years ago.
I have previously defined Report_wb in an module Obtain_Data, and now I am writing a new module Module1. I want to use the object Report_wb without defining it again. Is there any way to do that? Thanks!
rather than declaring Report_wb as a dim within a sub, write it as a public declaration at the top of the module
i.e.
'Module 1
Public stringTest as string
Sub SetString()
stringTest = "Hello World!"
End Sub
and then in another module:
'Module 2
Sub TestString()
call SetString
debug.print stringTest
End Sub

Optional argument in VBA causes error while executing [duplicate]

This question already has answers here:
VBA does not accept my method calling and gives Compile error: Syntax error [duplicate]
(2 answers)
Closed 7 years ago.
I tried to create procedure that passes 2 arguments, mandatory and optional, before I added optional argument procedure was running correctly. Here is Code:
Sub a2(var As String, Optional num As Integer = 5)
MsgBox (num)
End Sub
Sub start_a2()
a2 ("null_text", 5)
End Sub
When I pass any second argument, running procedure start_a2 fails at 1st line: Sub start_a2(), VBA higlight this line with Yellow and returns Syntax error, but do not provide any details. Second argument is inproperely passed?
Does it work if you use Call? Such as
Sub start_a2()
Call a2("null_text", 5)
End Sub
Edit: Though the above will work, #SO's comment below is right on (Thanks!); you can just use
Sub start_a2()
a2 "null_text", 5
End Sub