How to use a variable in an Excel formula when using the module Spreadsheet:WriteExcel?

Posted on 2007-08-12
Last Modified: 2013-11-05
I'm using the Perl module Spreadsheet::WriteExcel.
I want to subsitute a variable for the cell rows, instead of writing each line like I did below??

Any ideas??

$worksheet->write(1, 3, '=IF(AND(F2="",H2="",J2=""),"",MAX(F2,H2,J2))', $format);
$worksheet->write(2, 3, '=IF(AND(F3="",H3="",J3=""),"",MAX(F3,H3,J3))', $format);
$worksheet->write(3, 3, '=IF(AND(F4="",H4="",J4=""),"",MAX(F4,H4,J4))', $format);
$worksheet->write(4, 3, '=IF(AND(F5="",H5="",J5=""),"",MAX(F5,H5,J5))', $format);
$worksheet->write(5, 3, '=IF(AND(F6="",H6="",J6=""),"",MAX(F6,H6,J6))', $format);
$worksheet->write(6, 3, '=IF(AND(F7="",H7="",J7=""),"",MAX(F7,H7,J7))', $format);
$worksheet->write(7, 3, '=IF(AND(F8="",H8="",J8=""),"",MAX(F8,H8,J8))', $format);
$worksheet->write(8, 3, '=IF(AND(F9="",H9="",J9=""),"",MAX(F9,H9,J9))', $format);
$worksheet->write(9, 3, '=IF(AND(F10="",H10="",J10=""),"",MAX(F10,H10,J10))', $format);
$worksheet->write(10, 3, '=IF(AND(F11="",H11="",J11=""),"",MAX(F11,H11,J11))', $format);
$worksheet->write(11, 3, '=IF(AND(F12="",H12="",J12=""),"",MAX(F12,H12,J12))', $format);
$worksheet->write(12, 3, '=IF(AND(F13="",H13="",J13=""),"",MAX(F13,H13,J13))', $format);
$worksheet->write(13, 3, '=IF(AND(F14="",H14="",J14=""),"",MAX(F14,H14,J14))', $format);
$worksheet->write(14, 3, '=IF(AND(F15="",H15="",J15=""),"",MAX(F15,H15,J15))', $format);
$worksheet->write(15, 3, '=IF(AND(F16="",H16="",J16=""),"",MAX(F16,H16,J16))', $format);
$worksheet->write(16, 3, '=IF(AND(F17="",H17="",J17=""),"",MAX(F17,H17,J17))', $format);
$worksheet->write(17, 3, '=IF(AND(F18="",H18="",J18=""),"",MAX(F18,H18,J18))', $format);
$worksheet->write(18, 3, '=IF(AND(F19="",H19="",J19=""),"",MAX(F19,H19,J19))', $format);
$worksheet->write(19, 3, '=IF(AND(F20="",H20="",J20=""),"",MAX(F20,H20,J20))', $format);
$worksheet->write(20, 3, '=IF(AND(F21="",H21="",J21=""),"",MAX(F21,H21,J21))', $format);
$worksheet->write(21, 3, '=IF(AND(F22="",H22="",J22=""),"",MAX(F22,H22,J22))', $format);
$worksheet->write(22, 3, '=IF(AND(F23="",H23="",J23=""),"",MAX(F23,H23,J23))', $format);
$worksheet->write(23, 3, '=IF(AND(F24="",H24="",J24=""),"",MAX(F24,H24,J24))', $format);

Question by:rkckjk
    LVL 17

    Expert Comment

    Do you mean:

    #!/usr/bin/perl -w
    use strict;
    my $count = 1;

    for (1..23) {
        print "\$worksheet->write($count, 3, '=IF(AND(";
        print "F$count=\"\",H$count=\"\",J$count=\"\"),\"\",MAX(F$count,H$count,J$count))', \$format)\n";
    LVL 2

    Author Comment

    your solution justs print outs the formula 23 times.

    I changed your code slightly to this:

    my $count = 1;
    for (1..23) {
        $worksheet->write($count, 3, '=IF(AND(F$count=\"\",H$count=\"\",J$count=\"\"),\"\",MAX(F$count,H$count,J$count))', $format);

    but it still doesn't work
    LVL 17

    Accepted Solution

    #!/usr/bin/perl -w
    use strict;
    use Spreadsheet::WriteExcel;

    my $workbook = Spreadsheet::WriteExcel->new("perl.xls");
    my $worksheet = $workbook->add_worksheet();

    my $format = $workbook->add_format();

    my $row = 1;
    my $col = 2;
    for (1..23) {
        $worksheet->write($row, 3, "=IF(AND(F$col=\"\",H$col=\"\",J$col=\"\"),\"\",MAX(F$col,H$col,J$col))", $format);

    Puts the formulas in the cells for me -- I haven't evaluated the accuracy of your fomula, but the code enters the requested formulas in cells D2 through D24...

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now