+ Reply to Thread
Results 1 to 6 of 6

Change this code so it appends text to a cell that is highlighted.

Hybrid View

  1. #1
    Registered User
    Join Date
    03-05-2011
    Location
    New York
    MS-Off Ver
    Excel 2016
    Posts
    84

    Question Change this code so it appends text to a cell that is highlighted.

    I found this code that identifies a cell that is filled with background color and then highlights the column header it is in. How can i change this so it appends text to the front of the cell that is shaded instead. So if the cell is yellow and says "Automotive" in it, after running the script it would say something like
    "**CHANGED** - Automotive"

    Sub TagColumns()
      Dim headers As Range, body As Range, col As Long, found As Boolean
    
      ' define the columns for the headers and body
      Set headers = ActiveSheet.UsedRange.Rows(1).Columns
      Set body = ActiveSheet.UsedRange.Offset(1).Columns
    
      ' iterate each column
      For col = 1 To headers.Count
    
        ' search for any color in the column of the body
        found = VBA.IsNull(body(col).DisplayFormat.Interior.ColorIndex)
    
        ' set the header to red if found, green otherwise
        headers(col).Interior.Color = IIf(found, vbRed, vbGreen)
      Next
    
    End Sub

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Change this code so it appends text to a cell that is highlighted.

    Hi,

    Perhaps the following immediately before the loop's 'Next' instruction

    If headers(col).Interior.Color = vbRed Then headers(col) = "**CHANGED** - " & headers(col)
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    03-05-2011
    Location
    New York
    MS-Off Ver
    Excel 2016
    Posts
    84

    Re: Change this code so it appends text to a cell that is highlighted.

    Quote Originally Posted by Richard Buttrey View Post
    Hi,

    Perhaps the following immediately before the loop's 'Next' instruction

    If headers(col).Interior.Color = vbRed Then headers(col) = "**CHANGED** - " & headers(col)
    Thanks but this is changing the column header. To be clearer I need to find any highlighted cell on the sheet and then change that specific cell and not the headers. I included this code b/c it was the only thing i could find that was close to what I was looking for.

  4. #4
    Valued Forum Contributor
    Join Date
    06-29-2014
    Location
    Australia
    MS-Off Ver
    MSO 365
    Posts
    1,104

    Re: Change this code so it appends text to a cell that is highlighted.

    Hello BeaglesBuddy,

    If I've understood your request correctly then the following may help:-


    Sub Test()
    
          Dim c As Range
    
    For Each c In Sheet1.UsedRange.Offset(1) '---->Change sheet name to suit.
          If c.Interior.ColorIndex <> xlNone Then
          c.Value = "**CHANGED**" & "-" & " " & c.Value
          c.Interior.ColorIndex = xlNone
          End If
    Next c
    
    End Sub
    The code will also clear the cell colour so that it is not over written with each subsequent running of the code.
    The code also assumes that row1 has headings and data starts in row2.

    I hope that this helps.

    Cheerio,
    vcoolio.

  5. #5
    Registered User
    Join Date
    03-05-2011
    Location
    New York
    MS-Off Ver
    Excel 2016
    Posts
    84

    Re: Change this code so it appends text to a cell that is highlighted.

    Quote Originally Posted by vcoolio View Post
    Hello BeaglesBuddy,

    If I've understood your request correctly then the following may help:-


    Sub Test()
    
          Dim c As Range
    
    For Each c In Sheet1.UsedRange.Offset(1) '---->Change sheet name to suit.
          If c.Interior.ColorIndex <> xlNone Then
          c.Value = "**CHANGED**" & "-" & " " & c.Value
          c.Interior.ColorIndex = xlNone
          End If
    Next c
    
    End Sub
    The code will also clear the cell colour so that it is not over written with each subsequent running of the code.
    The code also assumes that row1 has headings and data starts in row2.

    I hope that this helps.

    Cheerio,
    vcoolio.
    Perfect Thanks!

  6. #6
    Valued Forum Contributor
    Join Date
    06-29-2014
    Location
    Australia
    MS-Off Ver
    MSO 365
    Posts
    1,104

    Re: Change this code so it appends text to a cell that is highlighted.

    You're welcome BeaglesBuddy.
    I'm glad that I was able to help.

    Cheerio,
    vcoolio.

    P.S. BTW, thanks for the rep.!
    Last edited by vcoolio; 08-21-2018 at 10:49 PM. Reason: Add P.S.

+ 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. Replies: 1
    Last Post: 05-27-2014, 05:39 PM
  2. Change date based on highlighted cell
    By AJHAYES in forum Excel General
    Replies: 3
    Last Post: 01-19-2013, 11:12 PM
  3. Code to change cell text color when macro has been applied?
    By XxCMoneyxX in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-30-2012, 01:04 PM
  4. Replies: 9
    Last Post: 06-26-2012, 02:08 PM
  5. Replies: 1
    Last Post: 08-19-2009, 02:47 AM
  6. Text to columns VBA code to change delimiter if the cell value is in array
    By dopple in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-26-2008, 09:25 AM
  7. [SOLVED] How do you change the default colour a cell is highlighted in?
    By CMorris in forum Excel General
    Replies: 0
    Last Post: 12-14-2005, 06:50 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