+ Reply to Thread
Results 1 to 19 of 19

VBA code to apply text to a cell based on its shade color.

  1. #1
    Forum Contributor
    Join Date
    08-22-2013
    Location
    USA
    MS-Off Ver
    MS Office for Office 365 (Latest - 32-bit)
    Posts
    107

    VBA code to apply text to a cell based on its shade color.

    Hi I'd like to see if there is some simple VBA code I can use to says in sheet "Jan" find the first cell in column H that is shaded in yellow and apply the text to it that is in cell C2. I unfortunately can't use formulas directly on the sheet because all of the cells on the sheet end up as pasted values.

    Thanks for any help!!

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: VBA code to apply text to a cell based on its shade color.

    for each c in range("H:H").cells
    if c.interior.colorindex=??????? then
    c.text = range("c2").text
    exit for
    end if
    next c
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Forum Contributor
    Join Date
    08-22-2013
    Location
    USA
    MS-Off Ver
    MS Office for Office 365 (Latest - 32-bit)
    Posts
    107

    Re: VBA code to apply text to a cell based on its shade color.

    Quote Originally Posted by nathansav View Post
    for each c in range("H:H").cells
    if c.interior.colorindex=??????? then
    c.text = range("c2").text
    exit for
    end if
    next c
    Hi Nathan,

    Thanks for the help. I'm getting a "Compile Error: Variable Not Defined" error when trying to run that code. I enclosed it in Sub and End Sub, and filled in colorindex with 6. What else might I be missing? Sorry, I'm still a little new to all this. Thanks again!

  4. #4
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: VBA code to apply text to a cell based on its shade color.

    where does the error happen?

    dim c as excel.range

    for each c in range("H:H").cells
    if c.interior.colorindex=??????? then
    c.text = range("c2").text
    exit for
    end if
    next c

  5. #5
    Forum Contributor
    Join Date
    08-22-2013
    Location
    USA
    MS-Off Ver
    MS Office for Office 365 (Latest - 32-bit)
    Posts
    107

    Re: VBA code to apply text to a cell based on its shade color.

    Ok, added the dim c statement and not getting any more errors. Thanks for that. However I'm also not getting any results, and I imagine it's because my colorindex number isn't correct. Any idea what the value of that should be for the standard yellow shading?

    Thanks again,

  6. #6
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: VBA code to apply text to a cell based on its shade color.

    format it on the sheet. then in VBA in the immediate window, type activecell.interior.colorindex.

  7. #7
    Forum Contributor
    Join Date
    08-22-2013
    Location
    USA
    MS-Off Ver
    MS Office for Office 365 (Latest - 32-bit)
    Posts
    107

    Re: VBA code to apply text to a cell based on its shade color.

    Quote Originally Posted by nathansav View Post
    format it on the sheet. then in VBA in the immediate window, type activecell.interior.colorindex.
    "Compile Error: Expected: identifier or bracketed expression"

  8. #8
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: VBA code to apply text to a cell based on its shade color.

    :o)

    the full stop was the end of the line, so you need

    ? activecell.interior.colorindex

  9. #9
    Forum Contributor
    Join Date
    08-22-2013
    Location
    USA
    MS-Off Ver
    MS Office for Office 365 (Latest - 32-bit)
    Posts
    107

    Re: VBA code to apply text to a cell based on its shade color.

    Quote Originally Posted by nathansav View Post
    :o)

    the full stop was the end of the line, so you need

    ? activecell.interior.colorindex
    Ok great, thanks for sticking with me here - please continue to bear with me here if you will

    So it brings back 6 for the value of standard yellow which is what I thought it was. However I'm still not getting any results. This is the exact code I have:

    Please Login or Register  to view this content.
    No errors when I run it, but doesn't put the text from cell c2 into the one yellow shaded cell in column H. Any ideas? I've gone ahead and attached it for your reference.

    Thanks again!
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    08-22-2013
    Location
    USA
    MS-Off Ver
    MS Office for Office 365 (Latest - 32-bit)
    Posts
    107

    Re: VBA code to apply text to a cell based on its shade color.

    By the way, I have no idea if this makes any difference, but the yellow shaded cells in the Sheet are conditionally formatted.

  11. #11
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: VBA code to apply text to a cell based on its shade color.

    ive just ran it and its fine, so i think it might be,

  12. #12
    Forum Contributor
    Join Date
    08-22-2013
    Location
    USA
    MS-Off Ver
    MS Office for Office 365 (Latest - 32-bit)
    Posts
    107

    Re: VBA code to apply text to a cell based on its shade color.

    Quote Originally Posted by nathansav View Post
    ive just ran it and its fine, so i think it might be,
    ?.........

  13. #13
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA code to apply text to a cell based on its shade color.

    VBA can't evaluate colors set by conditional formatting. You have to build logic into the code that is the same as your conditional formatting if you want to use that in your code.

  14. #14
    Forum Contributor
    Join Date
    08-22-2013
    Location
    USA
    MS-Off Ver
    MS Office for Office 365 (Latest - 32-bit)
    Posts
    107

    Re: VBA code to apply text to a cell based on its shade color.

    Quote Originally Posted by JOHN H. DAVIS View Post
    VBA can't evaluate colors set by conditional formatting. You have to build logic into the code that is the same as your conditional formatting if you want to use that in your code.
    Ok, thank you for that info, John.

    Another way I can approach it is to apply the text to column H where column A of the same row contains "Grand Total". Might you be able to help me with such code?

    Many thanks!

  15. #15
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA code to apply text to a cell based on its shade color.

    Based on your previous code, maybe:

    Please Login or Register  to view this content.

  16. #16
    Forum Contributor
    Join Date
    08-22-2013
    Location
    USA
    MS-Off Ver
    MS Office for Office 365 (Latest - 32-bit)
    Posts
    107

    Re: VBA code to apply text to a cell based on its shade color.

    Quote Originally Posted by JOHN H. DAVIS View Post
    Based on your previous code, maybe:

    Please Login or Register  to view this content.
    Hi again John, thanks for the help.

    There must be something wrong with the line 'If Cells(c.Row, "A") = "Grand Total" Then' - I'm getting a "Run-time Error '424' -Object Required."

    Any ideas?

  17. #17
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA code to apply text to a cell based on its shade color.

    Try:

    Please Login or Register  to view this content.

  18. #18
    Forum Contributor
    Join Date
    08-22-2013
    Location
    USA
    MS-Off Ver
    MS Office for Office 365 (Latest - 32-bit)
    Posts
    107

    Re: VBA code to apply text to a cell based on its shade color.

    Quote Originally Posted by JOHN H. DAVIS View Post
    Try:

    Please Login or Register  to view this content.

    John, works perfectly!

    Thank you so very much for your help on this one!

  19. #19
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA code to apply text to a cell based on its shade color.

    You're welcome. Glad to help out and thanks for the feedback.

+ 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] Formula to use to shade a cell red if two separate conditions apply (in separarate cells)
    By Dhabitude in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 12-03-2013, 08:18 AM
  2. Replies: 1
    Last Post: 08-06-2013, 07:45 AM
  3. how do I apply 10% gray shade to a cell in Excel?
    By myraddin in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-14-2006, 01:50 AM
  4. change text color based on adjacent cell text color
    By matthewst in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-01-2005, 03:49 PM
  5. [SOLVED] is there a way I can have excel shade a cell a color based on tha.
    By Terry in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-25-2005, 11:06 AM

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