+ Reply to Thread
Results 1 to 9 of 9

Scan for background color in cells and clear them in VBA

  1. #1
    Registered User
    Join Date
    05-04-2023
    Location
    Karlstad, Sweden
    MS-Off Ver
    2019
    Posts
    5

    Scan for background color in cells and clear them in VBA

    Hi, ive been fighting with chatgpt for a while now with a VBA code i hope someone could help me with. This is what i told chatgpt: Write a code in vba so that a macro will unlock the worksheet "Ber?kningssnurra EU-perioder" and all its cells. Then scan for all cells that contains a background color within the same worksheet and clear their contents. Then it should scan the same worksheet again and lock the worksheet except for the cells with background color.

    It came up with this:
    Please Login or Register  to view this content.

    I have been trying to formulate the same question in a thousand ways but theres always errors. Does anyone have any clue?
    Last edited by AliGW; 05-05-2023 at 07:04 AM. Reason: SOLVED tag applied - no need to edit the thread title!

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: Scan for background color in cells and clear them in VBA

    Please read the yellow banner at the top of this page on how to attach a sample workbook.

  3. #3
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,246

    Re: Scan for background color in cells and clear them in VBA

    Before you start the second loop, set Locked to True in all cells of the used range.
    Please Login or Register  to view this content.
    Artik

  4. #4
    Registered User
    Join Date
    05-04-2023
    Location
    Karlstad, Sweden
    MS-Off Ver
    2019
    Posts
    5

    Re: Scan for background color in cells and clear them in VBA

    bok1.xlsm Here is a sample (i hope), i have conditions on the yellow background which vba doesnt seem to recognize that they change? Maybe it doesnt work and i have to do it some other way.

  5. #5
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2406 Win 11 Home 64 Bit
    Posts
    23,950

    Re: Scan for background color in cells and clear them in VBA

    Daniel

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

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] 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



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)

    However, if you continue to not use code tags, you can expect to have your thread BLOCKED until you add them yourself.
    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

  6. #6
    Registered User
    Join Date
    05-04-2023
    Location
    Karlstad, Sweden
    MS-Off Ver
    2019
    Posts
    5

    Re: Scan for background color in cells and clear them in VBA

    I get a compilation error on "Dim protection As WorksheetProtection" and when chatgpt adjusts it i get some new kind of error and it just rotates. When i cut down the code and tried bit by bit i noticed it had trouble selecting and knowing what cells currently had background color and it cleared cells without color. When i filled a cell with color it worked fine but cells that shifted colors depending on conditions doesnt work at all.

  7. #7
    Registered User
    Join Date
    05-04-2023
    Location
    Karlstad, Sweden
    MS-Off Ver
    2019
    Posts
    5

    Re: Scan for background color in cells and clear them in VBA

    Okey thank you!

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Scan for background color in cells and clear them in VBA


    Hi,

    you should better forget chatgpt and trust the Macro Recorder and the VBA help as well …

  9. #9
    Registered User
    Join Date
    05-04-2023
    Location
    Karlstad, Sweden
    MS-Off Ver
    2019
    Posts
    5
    Yea i managed to do some other kind of validation thanks to macrorecording so ill mark as solved. Scanning for conditional background colors remains a mystery to me☺️

    Quote Originally Posted by Marc L View Post

    Hi,

    you should better forget chatgpt and trust the Macro Recorder and the VBA help as well …

+ 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] Change background color of duplicate, clear duplicates & leave background color changed
    By Ajaxs87 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-06-2023, 12:09 AM
  2. [SOLVED] Clear Table Background color
    By 75Rupert in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 08-10-2022, 07:21 PM
  3. [SOLVED] How to clear content on the cell that has background color from conditional formatting?
    By putritersenyum in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-18-2016, 10:13 AM
  4. Change cell background color based on another cells background color
    By Queo in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 4
    Last Post: 06-10-2014, 05:28 AM
  5. Clearing formats doesn't clear the background color
    By abbassi in forum Excel General
    Replies: 2
    Last Post: 04-26-2014, 01:06 AM
  6. [SOLVED] Macro to change all cells with a certain background color in another background color
    By kevinvzandvoort in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-13-2012, 11:04 AM
  7. Match a cells background color to another cells background color
    By cmb3587 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-10-2011, 02:54 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