+ Reply to Thread
Results 1 to 9 of 9

VBA for comparing Columns

  1. #1
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Ventura, united States
    MS-Off Ver
    Excel 2010
    Posts
    346

    VBA for comparing Columns

    This macro is supposed to look down column B and see if the value in each cell is greater than the value to the right(in column C). If so, it should color the cell. But it doesn't work!!! Help

    Please Login or Register  to view this content.
    Last edited by vlady; 12-04-2012 at 08:53 PM. Reason: code tags

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA for comparing Columns

    Where is the loop (i) in the code?

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA for comparing Columns

    Move out the count up, otherwise, you are going back to 2 each time you loop thr. Stack with 2 for ever

    Please Login or Register  to view this content.
    Last edited by AB33; 12-04-2012 at 06:46 PM.

  4. #4
    Registered User
    Join Date
    09-05-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: VBA for comparing Columns

    AB33's answer is correct. But why are you using two counter variables? You only need one

    Please Login or Register  to view this content.
    In the future you can solve these kinds of problems yourself by using VBA's debugger. Set a watch on the variable of interest (in this case count) and then step through the code by pressing F8. You'd see that the variable value kept being reset every time you re-entered the for loop.
    Last edited by DobbsHead; 12-04-2012 at 06:57 PM.

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA for comparing Columns

    With i

    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Ventura, united States
    MS-Off Ver
    Excel 2010
    Posts
    346

    Re: VBA for comparing Columns

    haha. That was a pretty retarded mistake. Thanks to everyone. Dobbshead, can you explain how to put a watch on a variable? I know how to execute the code line by line, but I don't know how to see what the variable is reading.

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA for comparing Columns

    Dobbs,
    My initial intention was not to came up with elegant answer but to show the OP where he/she went wrong so that to correct it themselves. Infact, I did not see your reply before I posted mine

  8. #8
    Registered User
    Join Date
    09-05-2012
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: VBA for comparing Columns

    Jiptastic,

    Sure, I'd be happy to explain how to use the watch and debugger.

    1) Open your code in the VBA editor

    2) Select the variable or expression you wish to watch. (i.e. you can watch the result of an expression, like: Range("A2").value)

    3) Right click and select "add watch". You will then see that variable appear in the current watches menu.

    4) Open the debug menu and select "Step Into", press F8 to see your code execute line by line

    That should get you started. You should experiment with the options in the debug menu. For example, you can add breaks to you code so that it stops at the same point every time, rather than having to step through everything.

    @AB33

    After reading my original post I realized it read snarkier than I intended, so I revised it. I apologize.

  9. #9
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Ventura, united States
    MS-Off Ver
    Excel 2010
    Posts
    346

    Re: VBA for comparing Columns

    DobbsHead. Thanks!! That will help alot in solving these sort of problems myself.

    AB33 thanks for answering my question on why it wasn't looping. Now I will watch out for that newbie mistake and I will stop using more variables than necessary so I can be more elegant as well.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1