+ Reply to Thread
Results 1 to 3 of 3

for loop to change the cell color didnt work

  1. #1
    Registered User
    Join Date
    11-05-2010
    Location
    london, england
    MS-Off Ver
    Excel 2003
    Posts
    59

    for loop to change the cell color didnt work

    Hi guys, I am trying to use a for loop to do some formatting work. however, the for loop seems to stop everytime I put in the line to change cell color.
    I have tested it with a simple loop, pls see the code below. Could anyone advise me why this happens?

    Thanks

    Sub test()

    Dim i As Integer

    For i = 1 To 20

    Range(Cells(1, 1).Offset(0, 0), Cells(1, 1).Offset(i, 0)).Select
    Selection.Interior.ColorIndex = i

    Next i

    End Sub

  2. #2
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: for loop to change the cell color didnt work

    Not sure what you're trying to do, but this will put the color indices 1 - 20 in column A:
    Please Login or Register  to view this content.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: for loop to change the cell color didnt work

    What doesn't work?

    As far as I can see, it extends the range one cell down each time through the loop and changes the colour to the value of i. That works. You don't need to select the cell(s) though:

    Please Login or Register  to view this content.

    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


+ 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