Thread: Macro???

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    43
    Rep Power
    15
    I want to perform the same test (if statement)on many variables.. Let's call them $count1, $count2, ....

    Is there a way to do this like a macro?

    Instead of having to type the same statement over and over, i figure there is a way to do this with a macro or some kind of loop.

    Thank you,

    Weldon Johnson
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    45
    Rep Power
    15
    Yes, there is a way to do it with loops, as long as your variable names are similar, in this example the variables are $count1,$count2,$count3.......etc

    for($i=1;$i<$someNumber;$i++)
    {
    $varname="count".$i;
    $var = $$varname;
    //this assigns the variable $var to $countN where N is a number
    if($var=="blah")
    do_something();
    }

    hope it helps
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    43
    Rep Power
    15
    Thanks. I assume this works, but why the
    $var = $$varname; line
    (and what does the double $$ mean)?

    Couldn't you leave out this line? and just do

    if($varname=="blah")
    do_something();
    }


    Just wondering. Thanks again.

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by polman:
    Yes, there is a way to do it with loops, as long as your variable names are similar, in this example the variables are $count1,$count2,$count3.......etc

    for($i=1;$i<$someNumber;$i++)
    {
    $varname="count".$i;
    $var = $$varname;
    //this assigns the variable $var to $countN where N is a number
    if($var=="blah")
    do_something();
    }

    hope it helps
    [/quote]

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    45
    Rep Power
    15
    Nope, see what happens is
    $varname = "count1" (for the first time through)
    if you did echo $varname, it'd be "count1", which isn't what you want. Basically the $$varname does $var = $("count1") and since $count1 is an assigned variable, it reads that value. It's kinda hard to explain but it's a very nice feature in php. Reply if you have more questions....I know php.net had a little blurb on it, which is how I know it (the hint is to look there for some more help ;-)).

Similar Threads

  1. Hiding Groups with VB macro in excel
    By dwoolley in forum Visual Basic Programming
    Replies: 4
    Last Post: August 1st, 2005, 04:52 PM
  2. Excel macro questions
    By Florad in forum Visual Basic Programming
    Replies: 2
    Last Post: September 27th, 2004, 03:29 PM
  3. macro, send msg, any program
    By techi in forum Visual Basic Programming
    Replies: 1
    Last Post: November 23rd, 2003, 03:07 AM
  4. Excel-Frequently used macro? Automating?
    By Ben Kingsley in forum Beginner Programming
    Replies: 2
    Last Post: October 13th, 2003, 09:57 AM
  5. VB Excel help needed: building a table via a macro
    By Jimfx in forum Visual Basic Programming
    Replies: 0
    Last Post: August 14th, 2003, 08:37 PM

IMN logo majestic logo threadwatch logo seochat tools logo