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

Your code doesn't run because - at least - only you have your directories and files. It's therefore impossible to reproduce your problem. The best I can do is to write some code that does work but reproduces my best guess at your situation. If you can give us working code that demonstrates your problem, I might be able to do better.

use strict; use warnings; use Win32::OLE; my $xl = Win32::OLE->new('Excel.Application'); $xl->{Visible} = 1; my $wb = $xl->Workbooks->Add; if ($wb->Sheets->{Count} > 1) { for (2..$wb->Sheets->{Count}) { $wb->Sheets(2)->Delete; } } $xl->VBE->ActiveVBProject->VBComponents->Add(1); my $cm = $xl->VBE->ActiveVBProject->VBComponents(3)->CodeModule; my $line = int($cm->CountOfLines); $cm->InsertLines(++$line,"Function Example(n as double) as double"); $cm->InsertLines(++$line,"Example = n^2"); $cm->InsertLines(++$line,"End Function"); my $sht = $wb->Sheets(1); $sht->Range("A1")->{Formula} = "=Example(2)"; $sht->Range("A2")->{Formula} = "=2*2"; my $a1 = $sht->Range("A1")->{Value}; my $a2 = $sht->Range("A2")->{Value}; if ($a1 != 4 or $a2 != 4) {print "We've had a problem"} else {print "L +ife is good"};

Regards,

John Davies

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

Replies are listed 'Best First'.
Re^2: Excel OLE cell value custome pre-defined predefined function formula error
by pooTan (Initiate) on Feb 12, 2011 at 01:41 UTC
    This works, I verified it.
Re^2: Excel OLE cell value custome pre-defined predefined function formula error
by pooTan (Initiate) on Feb 10, 2011 at 15:55 UTC

    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

      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.