I am trying to write a function in VBA, that I can then use in excel...
This is not the exact function that I want to create, but I'm having a lot of trouble so I simplified it down a lot...
Suppose I have the following matrix in my excel spreadsheet.
I know there is a built-in function in excel to find the determinate. Now, I want to use that in my function I define in VBA. Again there will be more stuff in my function, but I'm just trying to understand what I'm doing wrong.
So suppose this is my function I've defined in VBA...
Option Explicit
Public Function determinate(mtx As Range)
determinate = Application.MDeterm(mtx)
End Function
However, when trying to use determinate() in excel it doesn't calculate. Why is this/How can I fix this?
Related
I have created a UDF Excel via ALT+F11 in own Module.
Have read many tutorials where everything seems so easy (that you can use your UDF in each Cell of opened Workbook). But I can't use my function.
Tried two test functions in Module (Test and Test2).
Checked everything (disabled all Macro security, allow access to VBA project model) but I don't see my custom function when I type "=" in a cell.
I'm using Excel in active Office 365 license so it is the newest version.
What could cause that my UDF is not applicable in the Excel cell?
I just get "#NAME?" / "#VALUE?" as result and no auto suggestion of function name...
maybe anyone has a hint for me what could cause that issue. Thx!
Public Function test()
test = 1
End Function
Public Static Function test2()
test2 = 123
End Function
#RonRosenfeld,
This is to demonstrate what behaviour I'm seeing:
Scenario 1: Function pasted in ThisWorkbook calling it in Sheet1:
Scenario 2: Function pasted in Sheet2 calling it in Sheet1:
Scenario 3: Function pasted in Module1 calling it in Sheet1:
This is why I came to the conclusing the () must be missing to show the #NAME error since otherwise there would be a syntax error.
#OP, You seem to have put your function in a class module, instead of a regular module.
Functions located in a module created using: add a Module(second menu from left)/Module
succesfully shows up in the function menu.
I've got a function which uses InStr() to locate a character in a string, but I know there is a built in function in Excel called FIND(). Can anyone advise which is faster or more cpu efficient?
hours_position = InStr(1, value, " ")
vs
hours_position = Application.WorksheetFunction.Find(" ", value, 1)
Both are same and it does the same action.
Most (but not all) worksheet functions can also be called from VBA. For example, you can use the VLOOKUP worksheet function in VBA by calling Application.WorksheetFunction.VLookup (or Application.VLookup).
Similarly, you can use Application.WorksheetFunction.Find and Application.WorksheetFunction.Search. You can use them to emulate the way the worksheet functions work in your VBA code.
These functions are only available in Excel VBA, whereas InStr is a generic VBA function, available in all Office applications (and in VB6, VB.NET etc.)
Apart from that, the Range object in Excel VBA has a Find method, and the Worksheet object has a Find object. These, however, serve a different purpose: you can't use them to search for text within a string, but to search for cells with specified content.
I want to change text direction in a cell from left-to-right to right-to-left using vb code.
Can any one guide me to do that?
thanks.
It is easy, you just need to create a Function in Excel with the following code:
Function Reversestr(str As String) As String
Reversestr = StrReverse(Trim(str))
End Function
Then just use the function inside a cell like
=Reversestr("Text you want to reverse")
I want to write a little logging function in an excel add-in that I will be calling from many different workbooks. I'd like to be able to just call it by passing only the log text, and the log function itself could handle the timestamp, workbookname, etc.
However, I cannot use either ThisWorkbook or ActiveWorkbook to determine which workbook was responsible for making the call, as Thisworkbook will return a reference to the add-in itself, whereas VBA code running in a workbook other than the workbook with active focus in Excel could make the call, but the ActiveWorkbook will return the one that has focus in the window.
Application.Caller looked like a possible solution, but this seems to work only when the function is called from a cell, not from VBA.
Is what I'm trying to do impossible?
Update
According to > 1 person, this is in fact impossible. If anyone happens to know some clever workaround please speak up.
Ok, so having read the question properly I'll try again...
So, to state the problem:
you want a routine written in an addin, that when called from vba in another workbook can work out (among other things) which workbook contains the vba that made the call, without having to pass this information explicitly.
As stated this is not possible (this is a similar question to accessing the call stack from code: something that is to my knowledge not possible)
However you can almost get what you want like this
Declare your log function like this:
Sub MyLogger(wb as Workbook, LogText as String)
Dim CallerName as String
CallerName = wb.name
' your code...
End Sub
Then wherever you call the sub use
MyLogger ThisWorkbook, "Log Text"
Not quite as good as passing nothing, but at least its always the same
To get the name of the calling workbook, use
Application.Caller.Worksheet.Parent.Name
Application.Caller returns information about how Visual Basic was called. If called from a custom function entered in a single cell, a Range object specifying that cell is returned
Having got a reference to the cell, .Worksheet.Parent.Name gives you the name of the workbook
Note that Application.Caller will return other things depending on how your function is called (see VBA help for details)
In an Add-In Function called by an Excel Worksheet Array Entered Function Call, I find that "Application.Caller.Parent.name" gives the Sheet Name (Tab Name, not sheet number).
I had the same issue when coding a custom function. Function works well, but anytime another workbook is calculated or activated, all cells using that function revert to #value. It can be very frustrating when working with multiple files using this formula.
To get the Workbook I used:
Dim CallingWb As Workbook
Set CallingWb = Application.Caller.Parent.Parent
This should work if your function is in a cell.
Too late for the original post, but might help others!
I want to write a Excel function like this. It is an extension of the Dec2Bin function
Public Function Dec2BinEx(x As Long)
Dec2BinEx = dec2bin(x) + 10
End Function
But I am getting an error when trying to use it. How do I call a excel function inside the visual basic editor
In general, you call Excel functions with Application.WorksheetFunction.SomeFunctionName. However, Dec2Bin is special, as it is an Add-In function, and not a pure Excel function. Hence, Application.WorksheetFunction does not work here. Instead, you have to make the functions of the add-in available to your code. To do so, follow these steps
In Excel, menu Tools/Add-Ins, make
sure that the add-in Analysis
ToolPak - VBA is imported.
Then, set a reference to this add-in
in your code: in the VBA editor, menu
Tools/References, add a reference to
atpvbaen.xls.
Then, your original code, as posted in your quesiton should work just fine.
You will first of all have to create a module eg from menu select Insert->Module. Then inside this module create a function named main. This function is run by default when code is run. Now inside this function call your own function like this:
Sub main()
Call Dec2BinEx(your_value_here)
End Sub
Public Function Dec2BinEx(x As Long)
Dec2BinEx = dec2bin(x) + 10
End Function
Having done that, make sure that you have the reference to dec2bin function or if you create that too. Thanks