Results 1 to 5 of 5

Thread: [RESOLVED] range testing

  1. #1

    Thread Starter
    Addicted Member habenero's Avatar
    Join Date
    Aug 2015
    Posts
    224

    Resolved [RESOLVED] range testing

    hello to all

    here is the code that is driving me crazy

    what I want to do is set two ranges and color test against

    a cell (values)

    the trouble is my 2nd defined range is testing the 1st range

    what am I doing wrong?

    and it seems to be another simple

    fix - know I got two ranges with the same name

    so how does one name a new range - kill the original

    without getting the "variable is already set"

    thanks for the help

    Jeff


    Sub Color_tester_Mega_powerball()
    Set myRange = range("A2:e6")
    For Each cell In myRange
    If cell.Value = range("A15") Then
    cell.Interior.ColorIndex = 4
    ElseIf cell.Value = range("B15") Then
    cell.Interior.ColorIndex = 4
    ElseIf cell.Value = range("C15") Then
    cell.Interior.ColorIndex = 4
    ElseIf cell.Value = range("D15") Then
    cell.Interior.ColorIndex = 4
    ElseIf cell.Value = range("E15") Then
    cell.Interior.ColorIndex = 4
    Set myRange = range("f2:f6")
    ElseIf cell.Value = range("f15") Then
    cell.Interior.ColorIndex = 4
    End If
    Next
    End Sub

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,444

    Re: range testing

    You run through the Cells A2 to E6
    If none of those cells equals Range("E15") you're never going to set your Range to F2:F6, because it's in an If-ElseIf-construct
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: range testing

    i have no idea, from the description, what your end game is
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Addicted Member habenero's Avatar
    Join Date
    Aug 2015
    Posts
    224

    Re: range testing

    I start off by defining one range (a2:E6) and do a conditional test will cells A15 to E15

    Then I want to define a new range (f2:F6) then conditional test with cell F:15

    Zvoni is giving me a nudge or a hint . I can see that and I also know VBA is telling me that I set two ranges with the same name

    I have tried to re-name the 2nd range and it does not work - VBA tells me that I have the range named twice - it has to be simple tweak of code but cannot get this code to behave the way I want to, I have looked for solutions all over the net but this programming (loops and conditionals) are my kyptonite.

    Thanks again for the replies

    Jeff

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: range testing

    there is no issue with using the same variable for 2 ranges, but not within a for each loop of the variable, as at that point the variable is locked

    you do not actually nee to use a variable to loop through a range as you can do like
    Code:
    For Each cell In range("A2:e6")
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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