I have a subroutine that looks like this. It has two parameters.
Sub Advance(ByRef listR() As String, rCount)
...code
End Sub
When I try to call this:
Advance listR:=theList, theCount
It gives me a compile error:
Expected: named parameter
Why is this? It works fine like...
Sub Advance(rCount)
...code
End Sub
And called via:
Advance theCount
Or also works with just the array parameter.
I believe that you can call your sub any of the following ways, but you can't name the first parameter and not name the second one:
Advance listR:=theList, rCount:=theCount
Advance rCount:=theCount, listR:=theList
Advance theList, rCount:=theCount
Advance theList, theCount
Call Advance(listR:=theList, rCount:=theCount)
Call Advance(rCount:=theCount, listR:=theList)
Call Advance(theList, rCount:=theCount)
Call Advance(theList, theCount)
From MSDN:
When you supply arguments by a mixture of position and name, the positional arguments must
all come first. Once you supply an argument by name, the remaining arguments must all be by
name.
Related
Sub Main()
Console.WriteLine("check")
Console.Read()
End Sub
Why does Sub Main () need them? How do they apply to this procedure?
.WriteLine("") here i am adding a value.
Console.Read() is this holding the value "check" to show on console? Why why are they here.
I know all you experts think this is a dumb question, however i can not wrap my head around it help! To me these are boxes that hold or pass a procedure value.
Is sub main a container holding the code the uses enters? If so, why is it that when a form button is used it is full? But here a VB default unused and empty? Seems to me with no event values it should not be there....?????
Parenthesis are required when they are required, and optional when they are optional. In the case of empty parameter/argument lists parenthesis are "just for show".
A Sub Procedure can be declared as Sub Main() or Sub Main - the parenthesis are optional when there are no parameters. Likewise, procedures/functions can be invoked without parenthesis if (and only if) no arguments are supplied.
Sub A ' ok, no parameter list - no need for parenthesis
Sub A() ' it's fine to use parenthesis anyway
Sub B(x as Integer) ' need parenthesis for parameter list
obj.A ' ok, no arguments - no need for parenthesis
obj.A() ' it's fine to use parenthesis anyway
obj.B(42) ' need parenthesis when arguments are specified
In the above, the definitions of A and invocations of A are equivalent as the parenthesis are optional in these cases.
When calling a method you do have a choice in VB whether you want to include the parentheses if there are no parameters. The same holds true for the definition of a method, be it a function or a sub.
See http://msdn.microsoft.com/en-us/library/dz1z94ha.aspx (Sub Statement on MSDN).
Calling a Sub Procedure
You call a Sub procedure by using the procedure name in a statement
and then following that name with its argument list in parentheses.
You can omit the parentheses only if you don't supply any arguments.
However, your code is more readable if you always include the
parentheses.
I want to have a faster process looking up cross references.
Right now I use VLOOKUP, and it works fine - but it takes time when it needs to be done multiple times everyday.
It is always the same sheet I use to lookup the cross references, so the only thing that changes is my input value in the VLOOKUP function.
Therefore I want a function where I only input 1 value to get the VLOOKUP value.
The idea is a function like:
=CROSS(ID)
where
CROSS = vlookup(ID, table_array, col_index_num,[range_lookup])
So the vlookup_value is replaced by ID.
I hope you can provide me with some answers - thanks in advance.
I have tried multiple different things, but with no success.
As I am new, I've googled and recorded macros to look for answers.
You could write a UDF (user defined function) for that, using the WorksheetFunction.VLookup method:
Option Explicit
Public Function CROSS(ID As Variant) As Variant
CROSS = Application.WorksheetFunction.VLookup(ID, table_array, col_index_num, range_lookup)
End Function
I got it working as it should now!
The code ended up like this:
Sub crossref()
Option Explicit
Public Function CROSS(ID As Variant) As Variant
CROSS = Application.WorksheetFunction.VLookup(ID, Worksheets("Sheet1").Range("E:F"), 2, 0)
End Function
I am trying to run a macro with variables as follows:
Range("A1").Application.Run (SetConditionalFormatingSub,ConditionB="=""O-BETTER-T-PRV""",ConditionW = "=""O-WORSE-T-PRV""",ConditionM = "=""O-MIXED-T-PRV""")
But I get an error as follows:
Compile error
Expected =
Still a novice in excel coding, can't figure out what seems to be the problem.
Hopefully you guys can help! Thanks in advance.
Try:
Range("A1").Application.Run "SetConditionalFormatingSub", "='O-BETTER-T-PRV'", "='O-WORSE-T-PRV'", "='O-MIXED-T-PRV'"
Notes:
don't use parentheses () when calling a sub;
you can't use names for sub parameters (like ConditionW) with Run method - it only accepts arguments by position (the same order they are defined in your macro);
you can't use quotes inside quotes. Instead, use single quotes, for example " 'Hello' ", instead of " "Hello" "
I don't think you need an Application.Run method for your task. You can set conditional formatting for your range directly using FormatConditions.Add method:
FormatConditions.Add Method
I'm trying to learn a little VBA an I'm in the process of saving ranges as CSV files.
However, I'm having trouble with this line of code
Call SaveRangeAsCSV(SelectDown("B5:D5"), "C:\Test\test.csv", True)
And here is a snippet of the SelectDown function.
Private Function SelectDown(Range As String) As Range
SelectDown = Range(Range, ActiveCell.End(xlDown)).Select
End Function
I get the error: Expected array. I cannot seem to understand what the problem is. Any help would be appreciated :-)
It sounds like the function SaveRangeAsCSV is expecting an array, but you are passing in a Range. This function is not a built-in Excel function, so I can't check it. Maybe you could check what arguments it is expecting?
My function now looks like this and is working perfectly.
Private Function SelectDown(RangeAddress As String) As Range
Set SelectDown = Range(RangeAddress, ActiveCell.End(xlDown))
End Function
I create a Public vba function with 2 parameters(module).
When I call the function I type "=InvoiceAmount2(A9;B9)".
The first parameter turns blue. The second black.
I remake the same function using one parameter, the second I use into the function, that way it´s ok. But I need two parameters
This is how you call a user defined function with two parameters:
=MyFunction(A1,B1)
Sample Code:
Function MyFunction(rCellA As Range, rCellB As Range)
MyFunction = rCellA.Value + rCellB.Value
End Function
I found the problem.
I call the udf function in formula constructor. I informed the parameters (A9 and B9)
finally, the udf function filled the cell. "=InvoiceAmount2(A9;B9)".