Can someone explain to me how to make a custom function.
Like a function that takes and returns something.
Printable View
Can someone explain to me how to make a custom function.
Like a function that takes and returns something.
Like this:
VB Code:
'Function FunctionName(arguments) as ReturnType Function FunctionName(Arg1 As Long, Arg2 As String) As String ' ''Function body ' FunctionName = ReturnData ' <-- returns ReturnData to caller End Function
it's really easy!
Public Function function_name(var1 as integer, var2 as string) as integer
in the function you can have some calculations. at the end u just write
function_name = solution
End Function
to call a function you use the function like this
variable = function_name(var1, "abc")
or
if function_name(var1,"abc") = true then
You can do it within the Form (General) or you create a module
If you have a MSDN try to look for
Function Statement Visual Basic for Applications Reference
Details are well there.
Ok, I tried one. What is wrong with this it never stops looping when I use it.
Code:Function LoopSend(txt As String, i As Integer)
Dim i2 As Integer
i2 = 0
Do While (i2 < i)
SendKeys txt
i2 = i2 + 1
Loop
End Function
Based on what you posted, it is difficult to say.
What did you pass it for parameters, i.e., what does i equal? What does txt equal?
Also, functions return a value but your function is not set up that way. Basically, what you have is a Sub. To be a true function, you would declare is as something like[Highlight=VB]Private Function LoopSend(txt As String, i As Integer) As ? - what do you want returned? A string? An integer? A long?
Why don't you use a For loop? That's what it's for. And be sure to use [VBCODE] tags, not [CODE] tags for VB code ;)
Also indenting your code makes it easier to read and debug. VB.NET automatically
indents it for you, thank God.
VB Code:
Function LoopSend(txt As String, ByVal i As Long) As Boolean Dim i2 As Long 'Always best to work with Longs cause its the fastest 'computing data type and there's no worry for overflow. For i2 = 0 To i SendKeys txt Next i LoopSend = True End Function
Ok thanks works.