+ Reply to Thread
Results 1 to 7 of 7

IF statement to test the contents of a cell

  1. #1
    Registered User
    Join Date
    01-21-2010
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2003
    Posts
    4

    IF statement to test the contents of a cell

    Hi,

    I'm reasonably new to VBA and need some what I am hoping is simple help.

    I want to write and IF statement to test the contents of a cell in row 2 of a selected column. So for example, if I have L15 selected, I want the IF statement to test L2.

    I can do the IF statement fine, I just don't know how to ask it to reference row 2 of the current column selected (which may vary).

    Any ideas??

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,886

    Re: IF statement to test the contents of a cell

    You can use:
    Please Login or Register  to view this content.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    01-21-2010
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: IF statement to test the contents of a cell

    Excellent, many thanks for the prompt reply.

  4. #4
    Registered User
    Join Date
    01-21-2010
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: IF statement to test the contents of a cell

    Hi,

    I am going to make this slightly more complex now I think. I want to run this IF statement against a range of cells, but for each cell in the range (covering multiple columns) I want it to refer to row 2 of that column. This is what I have so far, but it ain't working...

    Please Login or Register  to view this content.
    Last edited by NBVC; 01-21-2010 at 12:37 PM.

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,886

    Re: IF statement to test the contents of a cell

    First question:
    Why are you using the Worksheet_Change event to loop through an entire range? (there may be a good reason, but I like to check!)
    Second, I think your problem is the reference to Activecell in the middle of the code:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    01-21-2010
    Location
    Milton Keynes, England
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: IF statement to test the contents of a cell

    Thanks, the change certainly fixed the problem...

    In answer to your first question...I don't know. Is there a better way of doing this? I am using VB to perform this task because of the number of conditions I want to check (on top of the one above). I was given code to allow me to do this, which is what has been modified (from the first IF) to fit my needs.

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,886

    Re: IF statement to test the contents of a cell

    The Change event fires when you change a value on a worksheet. Typically, your code would respond by only checking the relevant cells (e.g. the changed cells, or cells on the same row for example) rather than looping through every cell in that range every time any cell on the sheet is changed.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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