Error In Function Name - vba

I get an error when trying to use the below function. The strange part is that the error occurs in the name
Code:
Function DATECHECK(rng As Range, date_time As Date) As Boolean
For Row = 1 To rng.Rows.Count
'Stuff'
Next Row
End Function
Syntax:
DateCheck(Sheet2!A2:B561, Sheet1!A2)
Error:
#NAME
#NAME(Sheet2!A2:B561, Sheet1!A2)

Make sure the function is in a public module; not a private worksheet code sheet.
DateCheck should return something. Add DateCheck = 1 before End Function.
You cannot manipulate values of other cells with a worksheet UDF. You decided that showing the relevant code wasn't important; it was important.
In your function's context, Row is a variable. You need to declare it as a long (e.g. dim row as long) if you are using Option Explicit.

Related

Passing two Ranges as a parameters and executing though excel sheet

I am trying to make a function that pass two range as a parameters, the code look something like this:
Function WhatEverFunction(range1 As Range, range2 As Range) As Integer
Dim column As ingeter
column = range1.Column
'make some stuff with the column
WhatEverFunction = range1.Value + range2.Value
End Function
This is how I execute the code and pass the parameters:
But this is the result:
Here are some of those corrections. Also, you are really just doing SUM so maybe consider the following:
Public Function WhatEverFunction(ByVal range1 As Range, ByVal range2 As Range) As Long
Dim myColumn As Long
myColumn = range1.column
'make some stuff with the column
WhatEverFunction = Application.WorksheetFunction.Sum(range1, range2)
End Function
Note #john Coleman's comment about what a udf cannot do. To be explicit, a UDF returns something to the cell it is placed in.
And your local settings might require WhatEverFunction = Application.WorksheetFunction.Sum(range1; range2)

VBA: Using a range returned from a function

I'm trying to use WorksheetFunction.Match in VBA, but I'm having issues in populating the lookup_array parameter. For the lookup_array, I'm using the output of another function -GetSubRange() as Range - that returns a range.
I've verified that my WorksheetFunction.Match() syntax works, and that my GetSubRange() function also works - when coded differently, I can get the address, select it, etc.
The result is that the function returns a #VALUE! error, which I've concluded is that the lookup_array parameter isn't referencing the range input properly.
Any ideas?
Here's what I'm trying to do:
Function GetValue(rng As Range, colName As String, key As String) As String
Dim rngSubRange As Range
Set rngSubRange = GetSubRange(rng, colName)
GetValue = WorksheetFunction.Match(key, rngSubRange, 0)
End Function
Function GetSubRange(rng As Range, colName As String) As Range
Dim rngHeader As Range
Dim colNum As Integer
Set rngHeader = ThisWorkbook.Names(rng.Name.Name &"_Header").RefersToRange
colNum = Application.Match(colName, rngHeader, 0)
Set GetSubRange = rng.Columns(colNum)
End Function
The error was in the algorithm: I was searching in the wrong subrange. rngSubRange was getting the range of the value column, but the key was in another column. I had to create to subranges: one for the key column and one for the value column.

VBA Excel: How to pass one parameter of different types to a function (or cast Int/String to Range)?

I'm writing some VBA functions in Excel that compute word values and cross sums of the input.
I'm passing the input as Public Function cross_sum(myRange As Range) As Integer to them so that they take cell references as input, e.g. =cross_sum(A1). Works fine.
However when I try to chain two functions like =cross_sum(word_value(A1)) I run into th VALUE error because word_value() returns an Integer value and not the Range cross_sum() is set to expect. However I did not find a way to cast an Integer (or String) into a Range.
As Excel's built-in functions support chaining as well as Range input I wonder how.
Unfortunately this is my first VBA project so I wonder if and how to cast or what type to choose to get this working both ways.
Any pointers appreciated!
TIA,
JBQ
You can pass Variant to a function and the function can determine the type of input:
Public Function Inputs(v As Variant) As String
If TypeName(v) = "Range" Then
MsgBox "you gave me a range"
Else
MsgBox "you gave me a string"
End If
Inputs = "done"
End Function
Sub MAIN()
Dim st As String
Dim rng As Range
st = "A1"
Set rng = Range(st)
x = Inputs(st)
x = Inputs(rng)
End Sub
Without your code, it is hard to know what you could change. That being said...
There is not a way to convert an integer to a range. You would have to create a function to do so if that is what you desired.
You could create a converter function, maybe titled IntegerToRange, that takes an integer and after some logic (maybe 1 = "A1", 2 = "A2" or something), will return a range. Your cell formula would then be =cross_sum(IntegerToRange(word_value(A1))
Alternatively, you could modify your word_value function to return a range instead of an integer. Your cell formula would then be =cross_sum(word_value(A1).

VBA : How to change the cell's value in a function?

I'm a new vba programmer and I have some trouble.
This is my function :
Function CopiePaste(CASEREF As Range, REF)
Dim o As Range
For Each o In CASEREF
o.Value = REF
Next
End Function
I want define multiple cell's value in a function but my code doesn't work and I don't understand why ?
Thanks in advance
In VBA, you would use a Sub rather than a Function
Here is one way that a Sub could use a Function to accomplish this:
Sub MAIN()
Dim MSG As String
MSG = CopiePaste(Range("A1:A10"), 123)
MsgBox MSG
End Sub
Function CopiePaste(CASEREF As Range, REF) As Variant
Dim o As Range
For Each o In CASEREF
o.Value = REF
Next
CopiePaste = "Mission Accomplished!"
End Function
Note: the custom function does not actually appear in a cell, but rather is called from the sub.
I will make some suggestions but I am not an expert either :-)
First: a function is supposed to return a value to the place in the code where the function is called from.
Here, you want to do something instead of returning a value, so you should use a sub() instead of a function.
Second, I think you should also declare the "REF" variable for it to work, (unless if it's a public variable).

Constant in VBA referencing a label

New to VBA. Was trying to create a constant that references a named column in my worksheet and am getting an error. Is this something you can do in VBA or is my syntax just wrong?
Example:
Public Const ColNum As Integer = [SomeColumn].Column
A constant must be able to be evaluated at the time the code is compiled (ie. before it runs)
This is OK:
Const A as Long = 10 'constant value
and this:
Const B As Long = A 'from another constant
or even
Const B As Long = A * 10 'constant expression
but not this:
Const B As Long = ActiveSheet.Columns.Count 'errors
because ActiveSheet.Columns.Count can only be determined at runtime
The compile error tells you what's wrong: Constant expression required
In other words, as #roryap mentions, you can only use a literal value for a Constant expression, you can't assign it anything that must be evaluated at runtime. A possible workaround is to use constant strings (i.e., your range's Name) and assign elsewhere as needed
From your parent/main procedure, call on another procedure which will assign to the module-level or public variables
Option Explicit
Const MyColumnName as String = "Dave_Column"
Dim ColNum as Integer
Sub main()
Call InitializeVariables
'The rest of your code ...
MsgBox ColNum
End Sub
Sub InitializeVariables()
'Use this procedure to assign public/module scope variables if needed
ColNum = Range(MyColumnName).Column
End Sub
Alternatively, ColNum can be a function with optional parameters, which when left blank would return the range based on the Constant string, or you could specify a different range name/address to return another column number:
Option Explicit
Const MyColumnName as String = "Dave_Column"
Sub main()
MsgBox ColNum
MsgBox ColNum("H1")
End Sub
Function ColNum(Optional name$) As Integer
If name = vbNullString Then
name = MyColumnName
End If
ColNum = Range(name).Column
End Function
Note: this will fail if the named range doesn't exist :)