+ Reply to Thread
Results 1 to 7 of 7

For Each Loop code issue

Hybrid View

  1. #1
    Registered User
    Join Date
    05-05-2014
    Location
    Denver, CO
    MS-Off Ver
    O365
    Posts
    4

    For Each Loop code issue

    I know this code is very rough, I am very rusty, the 90s seem so recent. I cannot get the macro below to execute properly. It should go through each cell in a defined range read the value and change the cell background to correspond. The If Then nest works properly for each active cell, but cannot get it to loop through the range. Thank you in advance

    Sub Color()
    ' Changes the cell color to correspond to the letter grade
    Dim Grade As Range
    Set Grade = Range("L16:L33").Text
    Grade.Select
        
       For Each Cell In Grade
       
       If Cell.Value = "A" Then
       
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 5287936
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
      End If
      
      If Cell = "C" Then
       
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
      End If
      
      Next Cell
        
    End Sub
    Last edited by alansidman; 05-06-2014 at 10:02 AM. Reason: code tags

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,719

    Re: For Each Loop code issue

    Code Tags Added
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (Because you are new to the forum, I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    05-05-2014
    Location
    Denver, CO
    MS-Off Ver
    O365
    Posts
    4

    Re: For Each Loop code issue

    Thank you, I will begin using code tags. I do and will continue to search Google for applicable examples and sample code.

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: For Each Loop code issue

    Try:
    Sub Color()
    ' Changes the cell color to correspond to the letter grade
    Dim Grade As Range, Cell as range
    Set Grade = Range("L16:L33")
    For Each Cell In Grade
      If Cell.Value = "A" Then
        With Cell.Interior
          .Pattern = xlSolid
          .PatternColorIndex = xlAutomatic
          .Color = 5287936
          .TintAndShade = 0
          .PatternTintAndShade = 0
        end With
      End If
      If Cell = "C" Then
        With Cell.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 65535
    .TintAndShade = 0
    .PatternTintAndShade = 0
    End With
    End If
    Next Cell
    End Sub
    Best Regards,

    Kaper

  5. #5
    Registered User
    Join Date
    05-05-2014
    Location
    Denver, CO
    MS-Off Ver
    O365
    Posts
    4

    Re: For Each Loop code issue

    Awesome, Thank you worked perfectly. It looks like the issue was defining Cell? I will add to your reputation

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,719

    Re: For Each Loop code issue

    Try this:

    Option Explicit
    
    Sub Color()
    ' Changes the cell color to correspond to the letter grade
    Dim c As Range
    Dim Grade As Range
    Set Grade = Range("L16:L33")
    
        
       For Each c In Grade
       
       If c.Value = "A" Then
       c.Interior.Color = 5287936
     
        End If
      
      If c.Value = "C" Then
       c.Interior.Color = 65535
    
      End If
      
      Next c
        
    End Sub

  7. #7
    Registered User
    Join Date
    05-05-2014
    Location
    Denver, CO
    MS-Off Ver
    O365
    Posts
    4

    Re: For Each Loop code issue

    Thank you, got it I will change status to closed

+ 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] Issue with For Each loop
    By ATLGator in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-16-2012, 09:01 PM
  2. For Loop Issue
    By pr4t3ek in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-12-2008, 10:40 AM
  3. Do while loop issue
    By Licquor in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-29-2008, 11:19 AM
  4. Loop issue
    By punter in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-13-2007, 03:18 PM
  5. Issue with Do Loop
    By Linking to specific cells in pivot table in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-24-2005, 06:05 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