Using a declared function in a module in a cell

mtheriault2000

Well-known Member
Joined
Oct 23, 2008
Messages
826
Hello again

Is it possible to use a function that as been declared in a module directly in a cell

Code:
Declare Function GV_GetNamedString Lib "C:\Program Files (x86)\TS Support\MultiCharts\GlobalVariable.dll" _
                                    (ByVal strElementLoc As String, ByVal strOther As String) As String
When I'm editing a formula in a cell, I see the function as available, but it give me an "#Value" error

Code:
Formula for cell ("D18") =GV_GetNamedString("DataString"; "Error")
In VBA, the formula work and the appropriate value is captured in a variable

Martin
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I suspect you might need an intermediate VBA function that simply calls the declared function.
 
Upvote 0
I suspect you might need an intermediate VBA function that simply calls the declared function.
Hummm you lost me :eeek:

The goal is to have a cell that will display the value of a global variable. This variable to be updated from an external application. Don't know it is is possible yet

In my VBA, i changed my declaration to be a Public function. Now, I don't get an error, but the text of my formula is displayed as the value of my cell

Martin
 
Upvote 0
Is the cell formatted as text? If so, format as General and re-enter the formula.
 
Upvote 0
Re: Using a declared function in a module in a cell Resolve

As suggested by Rorya

I succeeded to create an intermediary function
Code:
Public Declare Function GV_SetNamedString Lib "C:\Program Files (x86)\TS Support\MultiCharts\GlobalVariable.dll" _
                                    (ByVal strElementLoc As String, ByVal intSetValue As String) As Long
                                    
Public Declare Function GV_GetNamedString Lib "C:\Program Files (x86)\TS Support\MultiCharts\GlobalVariable.dll" _
                                    (ByVal strElementLoc As String, ByVal strOther As String) As String

' Here is the intermediate function

Public Function Call_GetNamedString() As String
   Dim String_i As String
   String_i = GV_GetNamedString("DataString", "error ")
   Call_GetNamedString = String_i
End Function

Now Cell ("D17") formula = Call_GetNamedString()
and the value is the appropriate one

Martin

P.S. RoRya, I ought you another beer "Mary" :beerchug:
 
Upvote 0
But, How do i force the update of the cell?

Value is updated only if i select the cell then press enter. At that time the function formula is called and the cell value is updated.

Is it possible to have it done automatically?

I will post a specific question for that

Martin
 
Upvote 0
Add:
Code:
application.volatile true
to your function and it will update whenever your workbook calculates.
 
Upvote 0
Thanks for the suggestion, but not working yet. I did add your line
Code:
Public Function Call_GetNamedString() As String
   Dim String_i As String
   Application.Volatile True
   String_i = GV_GetNamedString("DataString", "error ")
   Call_GetNamedString = String_i
End Function

The cell is updated only when i select it the press enter. Even the "calculate" button on the toolbar does not do it

Martin
 
Upvote 0
Now, the cell value is updated when the page is recalculated.

Is it possible to have done automatically? My calculate option is set to that.

Martin
 
Upvote 0
No because there's nothing to trigger it. You'd have to run a timer to update it periodically.
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top