in reply to Re: Excel OLE cell value custome pre-defined predefined function formula error
in thread Excel OLE cell value custome pre-defined predefined function formula error

Thanks everyone for helping. Not sure how to attach the excel file. If there is a way please let me know

here is the code in the VB components that I have. I can see that you produced a component on the fly and tested it, which is something I can test as well. But if you want add my custom made function to your excel file as well and test as well

Function Concat2(myRange As Range, Optional myDelimiter As String) Dim r As Range Application.Volatile For Each r In myRange If Len(r.Text) > 0 Then Concat2 = Concat2 & r & myDelimiter End If Next r If Len(myDelimiter) > 0 Then Concat2 = Left(Concat2, Len(Concat2) - Len(myDelimiter)) End If End Function

One thing that I just noticed which might be causing problem is Application.volatile. Let me search a bit on that to see if it is causing problems.Thanks very much for your assistance again

  • Comment on Re^2: Excel OLE cell value custome pre-defined predefined function formula error
  • Download Code

Replies are listed 'Best First'.
Re^3: Excel OLE cell value custom function formula error - drifting OT
by davies (Monsignor) on Feb 10, 2011 at 17:08 UTC

    This is a Perl forum, not VBA, so I'd be grateful for any guidance from those who think I shouldn't answer this here - or at least, in this much detail.

    I'm pretty sure from this that your problem is VBA, not Perl. Application.Volatile is unlikely to affect it - it merely determines when to recalculate the function. However, if you have a delimiter and a range of blank cells, you end up trying to reduce the length of a zero length string. Excel gets very bolshie at such treatment & throws an error, which is what I think you are seeing, although it may be something to do with dates, which Excel can handle in ways that confuses itself. I would rewrite your VBA thus:

    Function Concat2(myRange As Range, Optional myDelimiter As String) As +String Dim r As Range Application.Volatile For Each r In myRange If Len(r.Text) > 0 Then Concat2 = Concat2 & CStr(r.Text) & myDelimiter End If Next r If Len(myDelimiter) > 0 And Len(Concat2) > Len(myDelimiter) Then Concat2 = Left(Concat2, Len(Concat2) - Len(myDelimiter)) End If End Function

    The main differences are:
    I have defined the function as string, not let it default to variant.
    I have explicitly used r.Text, as otherwise you will not get r.Text but r.Value, which need not be the same.
    I have explicitly converted r.Text to string. This is essential. Try a simple spreadsheet with a date as the only value in the range passed to this function to see what I mean. However, I don't know how you want dates handled, so something else may be necessary.
    I have checked that I'm not doing anything daft when removing the trailing delimiter.

    Changes I haven't made:
    Calling variables "myWhatever" - I can't see what information the "my" conveys & would remove it.
    I don't think Application.Volatile serves a useful purpose - anything that would change this function's value would cause its recalculation, too - so I would remove it.

    There is, heartfelt thanks to the gods, no way to upload any file that isn't text. If you still can't solve your problem, please try to write the smallest Perl programme that runs from scratch and demonstrates the problem. By all means use my code as a starting point for creating Excel files on the fly.

    Regards,

    John

    Update: corrected minor typo.