+ Reply to Thread
Results 1 to 7 of 7

If Then Else in a For Next loop

  1. #1
    Registered User
    Join Date
    04-28-2016
    Location
    Atlanta, GA
    MS-Off Ver
    Office Pro 2016
    Posts
    12

    If Then Else in a For Next loop

    I'm trying to set the interior color of a cell as different than the cell above it unless the values are the same. I'm still learning VBA, and this site has been invaluable, but obviously I'm going to make a few mistakes.

    Here's the code that I came up with to try to accomplish my task:
    Please Login or Register  to view this content.
    This results in a "Next without For" error. Can someone please gently explain what I'm doing wrong?

    Thanks very much.

    I'm just realizing that even if I didn't get that error, I would still always get a random color. Hrmph.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: If Then Else in a For Next loop

    You have started two If Then Else End If Statements.

    But you have only closed one of them.

    Please Login or Register  to view this content.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Registered User
    Join Date
    04-28-2016
    Location
    Atlanta, GA
    MS-Off Ver
    Office Pro 2016
    Posts
    12

    Re: If Then Else in a For Next loop

    Well, that helps. It runs now, but each cell in the column is the same color. What I was hoping to do was assign a new color to a cell when the cell above had a different value, otherwise the color stays the same.

  4. #4
    Registered User
    Join Date
    12-06-2016
    Location
    India
    MS-Off Ver
    2007
    Posts
    72

    Re: If Then Else in a For Next loop

    Change

    Sub randomCellColor()

    Dim lrow As Long
    Dim color As Integer
    Dim count As Long
    color = 1


    lrow = Cells(Rows.count, 1).End(xlUp).Row
    color = Application.WorksheetFunction.RandBetween(1, 56)

    For count = 2 To lrow 'excludes header row?
    If Cells(count, 1).value <> Cells(count - 1, 1).value Then
    Cells(count, 1).Interior.ColorIndex = color + 1
    color = color + 1
    Else
    If Cells(count, 1).value = Cells(count - 1, 1).value Then
    Cells(count, 1).Interior.ColorIndex = color
    color = color + 1
    End If
    End If
    Next count

    End Sub

  5. #5
    Registered User
    Join Date
    04-28-2016
    Location
    Atlanta, GA
    MS-Off Ver
    Office Pro 2016
    Posts
    12

    Re: If Then Else in a For Next loop

    Ok, I think I've got it.

    Please Login or Register  to view this content.
    I needed to generate a new random number each time the loop executed or each cell would just be the same color value. Then I needed to say "If this cell value is the same as the one above it, make the colors the same."

    Thanks for all your help once again

  6. #6
    Registered User
    Join Date
    04-28-2016
    Location
    Atlanta, GA
    MS-Off Ver
    Office Pro 2016
    Posts
    12

    Re: If Then Else in a For Next loop

    One more refinement: I found that--just by random--a cell would sometimes have a different value but the same color, which defeats the purpose of trying to assign unique colors to unique values. I fixed it by adding another nested if, and I don't know how clean it is, but it works.

    Please Login or Register  to view this content.
    I'm sure there's a simpler way to define an array and then pick an element at random, but I couldn't make any of the solutions I found apply. Anybody have any ideas?

    Thanks everyone for getting me to this point. I'm finally starting to feel a little comfortable with all this!

  7. #7
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,996

    Re: If Then Else in a For Next loop

    Please Login or Register  to view this content.
    Ben Van Johnson

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] (Beginner help) For loop inside do loop that displays information from reference sheet.
    By lediable007 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-08-2016, 03:20 PM
  2. [SOLVED] Auto email loop with formatted table data breaking on second loop VBA
    By Who_else in forum Excel General
    Replies: 6
    Last Post: 03-24-2016, 06:22 AM
  3. [SOLVED] VBA to loop within a loop of multiple blocks of data (currentregion) ... Expert needed
    By Jim885 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-20-2016, 08:28 PM
  4. Find function in nested loop breaking down - not on first time through loop
    By adamstarr12 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-07-2014, 04:59 PM
  5. HOW TO: Pause loop, fix error on a popup UserForm, continue loop
    By AndyMachin in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-19-2014, 04:37 PM
  6. [SOLVED] Copy dynamically changing column and Paste using VBA Loop (Loop within Loop)
    By nixon72 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-12-2013, 12:46 PM
  7. Why did an inner loop variable start overwriting the outer loop range suddenly?
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-16-2012, 03:24 PM

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