Results 1 to 5 of 5

Thread: Using an If statement in vba for multiple records in a range

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    11

    Using an If statement in vba for multiple records in a range

    Hi all. I am using the following bit of code:

    Dim rngCalc As Range
    Set rngCalc = Range("AD2:AD" & Cells(Rows.Count, 1).End(xlUp).Row)
    rngCalc.Formula = "=(8-K2)"
    rngCalc.Value = rngCalc.Value

    In this case, the first cell to be replaced is AD2 and it continues for as many rows as I have. What I want to do is use some If statements in conjuction with this, and have these if statements run for each cell (AD2, AD3, AD4, etc)
    So in this case, if AD2 = 7 then xxxxxxxxxxxxxx
    And so on. Im not sure how to word it. Should it be 'if rngcalc.value = 7'?
    I tried that and got a type mismatch. Any help would be appreciated with this, thanks!!!!

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Using an If statement in vba for multiple records in a range

    If Range("AD2") = 7 then......
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    11

    Re: Using an If statement in vba for multiple records in a range

    Thank you for the reply.
    I tried that, but that only changed AD2. The other cells that = 7 remained at 7. Is there a way to get the code to check for every cell in the range?

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Using an If statement in vba for multiple records in a range

    Loop it...

    VB Code:
    1. For x = 2 to whatever (row count)
    2.    If range("AD" & x) = 7 Then
    3.      'do something
    4.    End If
    5. Next
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    11

    Re: Using an If statement in vba for multiple records in a range

    Outstanding. Thank you for your help!

Posting Permissions

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



Click Here to Expand Forum to Full Width