+ Reply to Thread
Results 1 to 6 of 6

Code highlights additional activecell in addition to what it has to highlight

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-24-2012
    Location
    Hollidaysburg, Pa
    MS-Off Ver
    Excel 2010
    Posts
    398

    Code highlights additional activecell in addition to what it has to highlight

    I have this excel sheet where : (file attached
    • Values in row 11 are compared with a value in Cells(7,6)
    • The code has to identify a value in row 11 by
    • Cells(rw5, 11) - Cells(7, 6) <= 20 And Cells(rw5, 11) - Cells(7, 6) > 0
    • After it identifies its color index and its characters have to be changed

    My problem:
    • Code works fine but it also highlights any cell that was first selected.
    • Eg. when you open up this sheet and click on a cell anywhere (say Rang A1) and then run this program. The code in addition to what I want it to do, also highlights Range A1
    • I guess it has to do with Activcell.select thingy

    Code Used
    Sub max()
    Dim rw5 As Long
    
    For rw5 = 17 To 46
    
    If Cells(rw5, 11) - Cells(7, 6) <= 20 And Cells(rw5, 11) - Cells(7, 6) > 0 Then Cells(rw5, 11).Select
    ActiveCell.Interior.ColorIndex = 6
    ActiveCell.Font.Color = vbBlack
    ActiveCell.Font.Bold = True
    'End If
    Next rw5
    
    End Sub
    Another concern..

    Doesn’t allow me to use END IF… is there a reason why it doesn’t allow me to use END IF in spite of having IF
    Attached Files Attached Files
    Last edited by subbby; 01-30-2015 at 11:24 AM. Reason: added excel sheet

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Code highlights additional activecell in addition to what it has to highlight

    Hi subby,

    Try the following tested and working code:
    Sub FindMax()
      'MAX is an Excel KEYWORD.  It is NOT RECOMMENDED to use Excel KEYWORDS as variables or function/Sub names.
    
      Dim r As Range
      Dim rw5 As Long
    
      For rw5 = 17 To 46
      
        Set r = Cells(rw5, 11)
    
        If r.Value - Cells(7, 6) <= 20 And r.Value - Cells(7, 6) > 0 Then
          r.Interior.ColorIndex = 6
          r.Font.Color = vbBlack
          r.Font.Bold = True
        End If
    
      Next rw5
    
      'Clear object pointer
      Set r = Nothing
    
    End Sub
    a. By eliminating 'Select', the other cell was not highlighted.
    b. When you had if ... then on the same line, Excel thought the if statement was completed.
    It takes a lot more space, but I almost always use the following if construction:
    If a = b then
      c = d
    Endif
    Lewis
    Last edited by LJMetzger; 01-30-2015 at 12:55 PM.

  3. #3
    Forum Contributor
    Join Date
    08-24-2012
    Location
    Hollidaysburg, Pa
    MS-Off Ver
    Excel 2010
    Posts
    398

    Re: Code highlights additional activecell in addition to what it has to highlight

    Thank you very much.. That worked....

    Thanks

    -Subbby

  4. #4
    Forum Contributor
    Join Date
    08-24-2012
    Location
    Hollidaysburg, Pa
    MS-Off Ver
    Excel 2010
    Posts
    398

    Re: Code highlights additional activecell in addition to what it has to highlight

    Thanks for the advise to !

    -Subbby

  5. #5
    Forum Contributor
    Join Date
    08-24-2012
    Location
    Hollidaysburg, Pa
    MS-Off Ver
    Excel 2010
    Posts
    398

    Re: Code highlights additional activecell in addition to what it has to highlight

    Quote Originally Posted by LJMetzger View Post
    Hi subby,



    a. By eliminating 'Select', the other cell was not highlighted.
    b. When you had if ... then on the same line, Excel thought the if statement was completed.
    It takes a log more space, but I almost always use the following if construction:
    If a = b then
      c = d
    Endif
    Lewis
    Just an add on requirement.

    I do have to change interior color index of ALL CELLS with a value in it under the r.value cell. I used
    Range(r.value, Range("k65536").End(xlUp))
    Giving me errors.

    Any idea on that ?

  6. #6
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Code highlights additional activecell in addition to what it has to highlight

    Hi,

    Your syntax is close, but just not quite right. You need Range(Range1,Range2), but You have Range(Value,Range2).

    Try:
    Sub FindMax()
      'MAX is an Excel KEYWORD.  It is NOT RECOMMENDED to use Excel KEYWORDS as variables or function/Sub names.
    
      Dim myRange As Range
      Dim r As Range
      
      Dim rw5 As Long
    
      For rw5 = 17 To 46
      
        Set r = Cells(rw5, 11)
    
        If r.Value - Cells(7, 6) <= 20 And r.Value - Cells(7, 6) > 0 Then
          ''''' r.Interior.ColorIndex = 6   'This NOT NEEDED NOW - the code below will set the Color of this cell
          r.Font.Color = vbBlack
          r.Font.Bold = True
          
          'I do have to change interior color index of ALL CELLS with a value in it under the r.value cell
          'I used Range(r.value, Range("k65536").End(xlUp))
          '
          'r is a Range Object.
          'r.Value is the Value of the Range object
          '
          'Find all cells with values in the Column below the Cell referred to by 'r'.
          'Set the ColorIndex of all values in the Range
          Set myRange = Range(r, Range("k65536").End(xlUp))
          myRange.Interior.ColorIndex = 6  
          
        End If
    
      Next rw5
    
      'Clear object pointers
      Set r = Nothing
      Set myRange = Nothing
    
    End Sub
    It seems like you may want to exit the loop, when you find your match. To do this you can use:
    Exit For
    Lewis

+ 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] Highlight activecell's row and column
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-27-2014, 02:22 PM
  2. Code that highlights a row
    By reignkystar in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-20-2013, 09:55 PM
  3. [SOLVED] Highlight Row of ActiveCell
    By kicker850 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-30-2013, 05:24 PM
  4. [SOLVED] Highlight input cells but print without the highlights
    By plamb in forum Excel General
    Replies: 3
    Last Post: 09-07-2012, 12:48 PM
  5. highlight activecell
    By flow23 in forum Excel General
    Replies: 10
    Last Post: 02-21-2006, 08:10 AM

Tags for this Thread

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