http://www.perlmonks.org?node_id=890964


in reply to Excel’s Select and Activate considered harmful

So when I want to automate something, and the macro recorder uses Select, how do I rewrite it not to use Select?

It's good to warn against harmful practise, but if you offer no alternatives, your effort will most likely be lost.

  • Comment on Re: Excel’s Select and Activate considered harmful

Replies are listed 'Best First'.
Re^2: Excel’s Select and Activate considered harmful
by davies (Prior) on Mar 02, 2011 at 11:52 UTC

    Create your own object and assign to that. To give an example, instead of

    $sht->Range("A1")->Select;
    use
    my $rng = $sht->Range("A1");
    Then, rather than Selection->, use $rng->. Obviously, it's better to use a descriptive name rather than $rng.

    Regards,

    John Davies

Re^2: Excel’s Select and Activate considered harmful
by dasgar (Priest) on Mar 04, 2011 at 06:29 UTC

    Usually in the code that I've written, I'm interested in reading from or writing to a cell and I don't need or want to keep a reference around for that cell. In my case, I just skip the selection step altogether with code like:

    my $value = $sht->Range("A1")->{Value};

    or

    $sht->Range("A1")->{Value} = $value;

    Just tossing out a suggestion (hopefully a correct and useful one) about how to avoid using Select.