excelperlformulawin32ole

HowTo write a formula in cell using perl WIN32::OLE


I try to insert a formula =IF(D2="";"";COUNTIF(D:D;D2)) in an Excel worksheet. Here is the code I'm trying:

$WorkSheet->Cells($Row2Use,'L')->{Formula} = "=IF(D".$Row2Use."=\"\",\"\",COUNTIF(D:D,D".$Row2Use.")"; # =IF(D2="";"";COUNTIF(D:D;D2))

I also tried using {Value} instead of {Formula} without success! What did I wrong


Solution

  • This following works for me. Code taken and modified from the link given by xxfelixxx.

    use Cwd 'abs_path';
    use Win32::OLE;
    use Win32::OLE qw(in with);
    use Win32::OLE::Const "Microsoft Excel";
    $Win32::OLE::Warn = 3;
    
    
    my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit');
    
    $Excel->{Visible} = 1;
    
    my $Book = $Excel->Workbooks->Add;
    my $Sheet = $Book->Activesheet;
    
    $Sheet->Range("A1")->{Value} = 1;
    $Sheet->Range("A2")->{Value} = 1;
    $Sheet->Range("A3")->{Formula} = "=SUMA(R[-2]C:R[-1]C)";