+ Reply to Thread
Results 1 to 14 of 14

Code to Change Tab Color if Data Exists

  1. #1
    Forum Contributor
    Join Date
    02-21-2013
    Location
    Phoenix, AZ
    MS-Off Ver
    Excel 2007
    Posts
    122

    Code to Change Tab Color if Data Exists

    Is there a code that will change the color of a tab if there is data on the worksheet?

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,890

    Re: Code to Change Tab Color if Data Exists

    The following code placed in the code module for a worksheet will change the tab color to red once data has been entered on the worksheet. It does not change it back again if the sheet subsequently has all data deleted.

    Please Login or Register  to view this content.
    If you want code in one place that does it for all worksheets, it's quite a bit trickier but I think it's possible.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    02-21-2013
    Location
    Phoenix, AZ
    MS-Off Ver
    Excel 2007
    Posts
    122

    Re: Code to Change Tab Color if Data Exists

    There are a total of 12 tabs in the workbook. I need 8 of them (soon to be 7) to change color if there is data on them.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,890

    Re: Code to Change Tab Color if Data Exists

    My best suggestion is to replicate the above code in each of the 8 (soon to be 7) sheets that you want to have the tab change color when data is added. You do not have to change the code except if you want the tabs to be different colors.

    I experimented with using a Class to handle events from any sheet but have not gotten it to work yet. Because you only want some of the sheets to have that behavior, that would be a problematic solution anyway.

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

    Re: Code to Change Tab Color if Data Exists

    Another option:

    Please Login or Register  to view this content.
    This will remove the Tab Color as well and operates on all Sheets within the workbook. If there are sheets you don't want included you need to place a line at the beginning. In the sample code above it excludes Sheet3
    Last edited by JOHN H. DAVIS; 09-06-2013 at 03:53 PM.

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,890

    Re: Code to Change Tab Color if Data Exists

    Quote Originally Posted by JOHN H. DAVIS View Post
    This will remove the Tab Color as well and operates on all Sheets within the workbook.
    Excellent suggestion, I had forgotten about that event!

  7. #7
    Forum Contributor
    Join Date
    02-21-2013
    Location
    Phoenix, AZ
    MS-Off Ver
    Excel 2007
    Posts
    122

    Re: Code to Change Tab Color if Data Exists

    Quote Originally Posted by JOHN H. DAVIS View Post
    Another option:

    Please Login or Register  to view this content.
    This will remove the Tab Color as well and operates on all Sheets within the workbook. If there are sheets you don't want included you need to place a line at the beginning. In the sample code above it excludes Sheet3
    I hit Alt+F11 and put this code into a new module however it is not working.

  8. #8
    Forum Contributor
    Join Date
    02-21-2013
    Location
    Phoenix, AZ
    MS-Off Ver
    Excel 2007
    Posts
    122

    Re: Code to Change Tab Color if Data Exists

    The tab names I need excluded would be the below.

    Client List
    Combined
    Correct File
    Research

  9. #9
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Code to Change Tab Color if Data Exists

    Quote Originally Posted by jscalem View Post
    I hit Alt+F11 and put this code into a new module however it is not working.
    Hi,

    This is a workbook event and has to be placed under "ThisWorkbook" module

    I have modified the code provided by John to exclude the specified sheets.

    Thanks

    Please Login or Register  to view this content.

  10. #10
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,890

    Re: Code to Change Tab Color if Data Exists

    I am getting an error on this line if the sheet is not completely blank. Using UsedRange in a comparison is failing.

    Please Login or Register  to view this content.
    I was able to get this modification to work.
    Please Login or Register  to view this content.

  11. #11
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Code to Change Tab Color if Data Exists

    Quote Originally Posted by 6StringJazzer View Post
    I am getting an error on this line if the sheet is not completely blank. Using UsedRange in a comparison is failing.

    Please Login or Register  to view this content.
    I was able to get this modification to work.
    Please Login or Register  to view this content.
    I just tested it and I dont get an error but the tab color does not change back to none. Its strange :?

  12. #12
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Code to Change Tab Color if Data Exists

    It works well for me.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  13. #13
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Code to Change Tab Color if Data Exists

    maybe its time for me to close the application completely and delete temp files to clean Excel garbage

  14. #14
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,890

    Re: Code to Change Tab Color if Data Exists

    There are multiple solutions posted close together with multiple members offering commentary but we can't tell who is responding to what.

    Let's wait for jscalem to review what's here and post back to tell us whether any of this is solving his problem.

+ 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: 0
    Last Post: 05-08-2013, 07:07 AM
  2. VBA Code to change row color
    By adamheon in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-07-2013, 05:53 AM
  3. [SOLVED] VBA code to change background color of a row in a spreadsheet
    By michelle27800 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-15-2012, 10:09 AM
  4. Best way to change color of font in code?
    By bobbby1949 in forum Excel General
    Replies: 5
    Last Post: 08-27-2011, 04:50 PM
  5. Change the excel Menu Color with code
    By Syed Haider Ali in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-14-2008, 06:15 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