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.

In reply to Re^3: Excel OLE cell value custom function formula error - drifting OT by davies
in thread Excel OLE cell value custome pre-defined predefined function formula error by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.