how do I use a excel function inside the vba editor - excel

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

Related

Saving Excel macro with Parameters in Subroutine [duplicate]

I am writing VBA macros in excel 2016. Macros I write that have arguments do not show up in the Macro Table, only the ones that have no arguments. Help.
Macros that take arguments are not visible in the macro box because there is no point in having them there. If they need arguments to run, they cannot be run from the macro box because there is no way to supply an argument to the macro in question.
Normally, a macro shows up in the macro list when you display the
Macros dialog box (press Alt+F8), unless one of three conditions is
met:
The macro is a function. Functions typically return information,
and they require information to be passed to them. Since running a
macro from the macro list doesn't allow either of these things to
happen, Excel figures there is no need to list it. User-defined
functions, which are quite useful in Excel, are not displayed in the
Macros dialog box because they are, after all, functions.
The macro is
a subroutine with parameters. Excel assumes that since parameters are
necessary, and you cannot provide parameters by choosing the
subroutine from the macro list, there is no need to list it.
The subroutine has been declared Private. This means that the subroutine
is only useful to code within the module in which it is declared.
Source.
Depending on your need, a possible workaround is to use a helper-sub like this:
Sub InvisibleMacro(strArg As String)
MsgBox("The passed argument was " & strArg)
' This macro won't be visible in the macro dialog because it can only be called with an argument
End Sub
Sub VisibleMacro()
Call InvisibleMacro("Abc")
' This macro will be visible in the macro dialog because it requires no arguments and is not private.
' It will call the "invisible" macro with a preset argument.
End Sub
You can use InputBox or the likes if you need the passed argument to be non-static. Of course, depending on what datatype you need to pass as an argument, this approach may be limited and/or require some extra hoops.
Obviously if the macro requires parameters they can't be passed by clicking an icon.
But I have a Sub that only has one Optional parameter and that doesn't show up in the list either when trying to attach it to an icon in the customized ribbon.
Comment out the full Sub declaration and substitute a line without parameters, attach the macro to an icon, then put back the real line. The icon will still work.
' Temporarily use the parameterless line to set things up, then put
' back the real line. I assume this would crash if the Sub has required
' parameters.
Sub MySub()
' Sub MySub(Optional ByVal MyParm As String)
If MyParm = "" Then MyParm = "No parameter"
MsgBox (MyParm)
End Sub
I know this is an older post but I had the same issue. My fix was that I had to open the macros in the VB editor and then they showed up in the macro list. from there I could add them to the ribbon and change the icons. Hope this helps.
The macro is not listed when it has parameters, but knowing the name you should be able to simply write the name and theirs parameters when Excel prompts in the Assign Macro window, using single and double quotes.
The following example will guide you how to do it:
'mymacro "param1"'
'mymacro TRUE, FALSE'
'mymacro "param1","param2"'
My problem was that I had defined Macro directly through Visual Basic Editor in Excel and had defined it under "Class Modules" folder instead of "Modules"
Once I moved it into Modules folder, it was visible.
Hope this helps someone.
You probably have made macro as "Function()". To make it visible in macros list, you have to declare it as "Sub()".

My Custom Excel UDF is not accessible (cell value = #NAME?)

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.

Using excel function in created VBA function

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?

Mac Excel 2011 VBA Evaluate

I am moving a excel VBA from PC to Mac and one line has an Evaluate function, for example:
Public Function test()
test = Evaluate("=2+2")
End Function
However, if I try and run the macro on the mac the function returns #name. Is there an equivalent function on the Mac side to Evaluate?
There is nothing wrong with the above function.
If you are calling the above function in VBA then it works as expected and If you are using it as a UDF i.e you are calling it from the worksheet then #Name error means that you have not pasted the function in a module. Move the function from the Sheet/Workbook code area into a module and then try again.

Possible to tell which workbook called a function in an Excel Add-In (xla)

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!

Resources