Hoppa till huvudinnehåll

Hur ändrar jag värde baserat på cellfärg i Excel?

Den här artikeln talar om att ändra cellvärde automatiskt baserat på dess bakgrundsfärg i Excel. Till exempel, om celler som fylls med röd bakgrundsfärg i markeringen, fyller du i dessa celler med nummer 1 och för de blå bakgrundsfärgscellerna fyller du i nummer 0.

Ändra värde baserat på cellfärg med VBA-kod


Ändra värde baserat på cellfärg med VBA-kod

Du kan köra nedanstående VBA-kod för att ändra värde baserat på cellfärg i Excel. Gör så här.

1. Välj det intervall du behöver för att ändra värden baserat på bakgrundsfärg och tryck sedan på andra + F11 samtidigt för att öppna Microsoft Visual Basic för applikationer fönster.

2. I Microsoft Visual Basic för applikationer fönstret klickar Insert > Modulerna, kopiera och klistra in nedanstående VBA-kod i modulfönstret.

VBA-kod: Ändra värde baserat på cellfärg i Excel

Sub ChangeValueBasedOnCellColor()
    Dim rg As Range
    Dim xRg As Range
    Set xRg = Selection.Cells
    Application.DisplayAlerts = False
    For Each rg In xRg
        With rg
            Select Case .Interior.Color
                Case Is = 255 'Red
                    .Value = 1
                Case Is = 15773696 'Blue
                    .Value = 0
            End Select
        End With
    Next
    Application.DisplayAlerts = False
End Sub

3. tryck på F5 för att köra koden, då kan du se att alla röda celler i det valda intervallet är fyllda med nummer 1 och de blå cellerna är fyllda med nummer 0 enligt nedanstående skärmdump.

 

Bästa kontorsproduktivitetsverktyg

🤖 Kutools AI Aide: Revolutionera dataanalys baserat på: Intelligent utförande   |  Generera kod  |  Skapa anpassade formler  |  Analysera data och generera diagram  |  Anropa Kutools funktioner.
Populära funktioner: Hitta, markera eller identifiera dubbletter   |  Ta bort tomma rader   |  Kombinera kolumner eller celler utan att förlora data   |   Rund utan formel ...
Superuppslag: Flera kriterier VLookup    VLookup med flera värden  |   VSök över flera ark   |   Fuzzy Lookup ....
Avancerad rullgardinslista: Skapa snabbt en rullgardinslista   |  Beroende rullgardinslista   |  Flervals-rullgardinslista ....
Kolumnhanterare: Lägg till ett specifikt antal kolumner  |  Flytta kolumner  |  Växla synlighetsstatus för dolda kolumner  |  Jämför intervall och kolumner ...
Utvalda funktioner: Rutnätsfokus   |  Designvy   |   Stor formelbar    Arbetsbok & Bladhanterare   |  Resursbibliotek (Automatisk text)   |  Datumväljare   |  Kombinera arbetsblad   |  Kryptera/Dekryptera celler    Skicka e-postmeddelanden efter lista   |  Superfilter   |   Specialfilter (filtrera fet/kursiv/genomstruken...) ...
Topp 15 verktygssatser12 text verktyg (lägga till text, Ta bort tecken, ...)   |   50+ Diagram Typer (Gantt Chart, ...)   |   40+ Praktiskt Formler (Beräkna ålder baserat på födelsedag, ...)   |   19 Införande verktyg (Infoga QR-kod, Infoga bild från sökväg, ...)   |   12 Konvertering verktyg (Siffror till ord, Valutaväxling, ...)   |   7 Slå ihop och dela verktyg (Avancerade kombinera rader, Dela celler, ...)   |   ... och mer

Uppgradera dina Excel-färdigheter med Kutools för Excel och upplev effektivitet som aldrig förr. Kutools för Excel erbjuder över 300 avancerade funktioner för att öka produktiviteten och spara tid.  Klicka här för att få den funktion du behöver mest...

Beskrivning


Fliken Office ger ett flikgränssnitt till Office och gör ditt arbete mycket enklare

  • Aktivera flikredigering och läsning i Word, Excel, PowerPoint, Publisher, Access, Visio och Project.
  • Öppna och skapa flera dokument i nya flikar i samma fönster, snarare än i nya fönster.
  • Ökar din produktivitet med 50 % och minskar hundratals musklick för dig varje dag!
Comments (11)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Is there a way to do this, but have it automatically go through multiple files?
For context, questionnaires were sent to a couple hundred facilities, their MC answer selected corresponds to a color indicating score (e.g. green is good, red is bad), and change the content of the cell (with a process like in this thread) to something that R or another software could read, and then use that to generate automatic reports. I could go through each of these files with this macro (or a completely different idea), but that would take a long time.
This comment was minimized by the moderator on the site
The code works fine for me, but I would need to rewrite the code to fit colours specific for my document. But I don´t know what code "my colours" have, anyone who know where on can find codes for other colours?
This comment was minimized by the moderator on the site
I came across this lucky strike which was helpful but don't know where to find a full directory!
http://cdn-0.access-excel.tips/wp-content/uploads/2015/08/excel_vbcolor_10.png 
This comment was minimized by the moderator on the site
How do you run it, i run but nothing seems to happen
This comment was minimized by the moderator on the site
You can replace colors to values by simply using Find and Replace function. Go to the format function on the right side of the Find and Replace dialog box and then under "Fill" you can select the color that you have. Then on the "replace with" just write the value you want.
This comment was minimized by the moderator on the site
is it possible to adjust this code based on the cells conditional formatting rule?
This comment was minimized by the moderator on the site
Excel seems to crash whenever I try to run the VBA code. Seems like a bust.
This comment was minimized by the moderator on the site
Good day,
The code works well in my case. Can you tell me your Excel version? Thank you for your comment.
This comment was minimized by the moderator on the site
I'm in the same boat, trying to run this but the code seems to get hung. Excel 2016.
This comment was minimized by the moderator on the site
I'm looking to give a cell a name based on the colour within one worksheet. i.e. if a cell is red then it gets named "name", if it is orange then it gets names "surname" etc.



What code would need to change to have the range as the whole sheet/tab and a name instead of a value?
This comment was minimized by the moderator on the site
What changes to the code would be needed if you wanted this to apply to the text color of a cell, rather than the cell color?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations