Salt la conținutul principal

Cum se schimbă culoarea filei de foaie pe baza valorii celulei?

În mod normal, puteți schimba culoarea filelor ușor și rapid în Excel, dar ați încercat vreodată să schimbați culoarea filelor pe baza valorii celulei dintr-o foaie de lucru? Dacă sunteți interesat de această sarcină, voi vorbi despre câteva coduri pentru ca dvs. să colorați fila de foaie pe baza unei valori specifice a celulei din Excel.

Schimbați culoarea filei unei foi pe baza valorii celulei cu codul VBA

Schimbați mai multe file de foi pe baza valorii celulei cu codul VBA


săgeată albastru dreapta balon Schimbați culoarea filei unei foi pe baza valorii celulei cu codul VBA

De exemplu, vreau ca culoarea filelor curente a foii să fie verde dacă valoarea celulei din A1 este textul „ADEVĂRAT”, culoarea filelor va fi roșie dacă textul din A1 este „FALS”, iar culoarea filelor va fi albastră dacă valoarea din celula A1 este orice alt text, după cum se arată în următoarea captură de ecran:

foaie de culoare doc după valoarea 1

1. Faceți clic dreapta pe fila de foaie pe care doriți să-i modificați culoarea pe baza datelor din celula A1, apoi alegeți Afișați codul din meniul contextual.

2. În Microsoft Visual Basic pentru aplicații fereastră, copiați și lipiți codul VBA de mai jos în Module fereastră.

Cod VBA: Modificați culoarea filei unei foi pe baza valorii celulei:

Private Sub Worksheet_Change(ByVal Target As Range)
'Updateby Extendoffice
    If Target.Address = "$A$1" Then
        Select Case Target.Value
        Case "False"
            Me.Tab.Color = vbRed
        Case "True"
            Me.Tab.Color = vbGreen
        Case Else
            Me.Tab.Color = vbBlue
        End Select
    End If
End Sub

foaie de culoare doc după valoarea 2

Notă: În codul de mai sus, A1 este referința celulei pe care doriți să colorați fila pe baza „Adevărat","Fals”Sunt textul de care ai nevoie, le poți schimba în funcție de nevoile tale și poți schimba culoarea din cod după cum ai nevoie.

3. Apoi salvați și închideți această fereastră de cod, acum, când introduceți textul „Adevărat” în celula A1, culoarea curentă a filei va deveni verde, iar când introduceți textul „Fals” în celula A1, culoarea filei va deveni roșie, iar dacă se introduce alt text în celula A1, culoarea filelor va deveni albastră automat.


săgeată albastru dreapta balon Schimbați mai multe file de foi pe baza valorii celulei cu codul VBA

Dacă trebuie să schimbați culoarea filelor cu mai multe foi pe baza unei valori a celulei, iată și un cod care vă poate ajuta, faceți următoarele:

1. Țineți apăsat butonul ALT + F11 tastele pentru a deschide Microsoft Visual Basic pentru aplicații fereastră, în fereastra deschisă, faceți dublu clic Acest manual de lucru în temeiul Proiect VBA , apoi copiați și lipiți codul de mai jos în spațiul gol Module:

Cod VBA: Modificați mai multe file de foi pe baza valorii celulei:

Option Explicit
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
  'Updateby Extendoffice 20160930
  Select Case Sheets("Master").Range("A1").Value
         Case "KTE"
             Sheets("Sheet1").Tab.Color = vbRed
         Case "KTO"
             Sheets("Sheet2").Tab.Color = vbGreen
         Case "KTW"
             Sheets("Sheet3").Tab.Color = vbBlue
         End Select
End Sub

foaie de culoare doc după valoarea 3

Notă: În codul de mai sus, A1 și Master sunt celula și foaia de lucru pe care doriți să colorați fila în funcție de, Sheet1, Sheet2, Sheet3 sunt foile de lucru pe care doriți să le colorați filelor. KTE, KTW, OMS sunt valorile celulei din A1 pe care doriți să colorați filele pe baza, puteți modifica referințele și culorile din cod după cum aveți nevoie.

2. Apoi salvați și închideți această fereastră de cod, acum, când introduceți textul KTE în celula A1 a foii principale, fila Sheet1 va fi colorată în roșu, când introduceți KTO în celula A1, Sheet2 va fi colorată în verde și când introduceți KTW în celula A1, Sheet3 va fi colorată în albastru, vedeți captura de ecran:

foaie de culoare doc după valoarea 4

Cele mai bune instrumente de productivitate de birou

🤖 Kutools AI Aide: Revoluționați analiza datelor pe baza: Execuție inteligentă   |  Generați codul  |  Creați formule personalizate  |  Analizați datele și generați diagrame  |  Invocați funcțiile Kutools...
Caracteristici populare: Găsiți, evidențiați sau identificați duplicatele   |  Ștergeți rândurile goale   |  Combinați coloane sau celule fără a pierde date   |   Rundă fără Formula ...
Super căutare: VLookup cu mai multe criterii    VLookup cu valori multiple  |   VLookup pe mai multe foi   |   Căutare fuzzy ....
Listă derulantă avansată: Creați rapid o listă derulantă   |  Listă drop-down dependentă   |  Listă derulantă cu selectare multiplă ....
Manager de coloane: Adăugați un număr specific de coloane  |  Mutați coloanele  |  Comutați starea vizibilității coloanelor ascunse  |  Comparați intervale și coloane ...
Caracteristici prezentate: Focus pe grilă   |  Vedere de proiectare   |   Big Formula Bar    Manager registru de lucru și foi   |  Biblioteca de resurse (Text automat)   |  Data Picker   |  Combinați foi de lucru   |  Criptare/Decriptare celule    Trimiteți e-mailuri după listă   |  Super Filtru   |   Filtru special (filtrează bold/italic/barat...) ...
Top 15 seturi de instrumente12 Text Instrumente (Adăuga text, Eliminați caractere,...)   |   50+ Diagramă Tipuri de (Gantt Chart,...)   |   40+ Practic Formule (Calculați vârsta pe baza zilei de naștere,...)   |   19 inserare Instrumente (Introduceți codul QR, Inserați imaginea din cale,...)   |   12 Convertire Instrumente (Numere la cuvinte, conversie valutara,...)   |   7 Merge & Split Instrumente (Rânduri combinate avansate, Celule divizate,...)   |   ... și altele

Îmbunătățiți-vă abilitățile Excel cu Kutools pentru Excel și experimentați eficiența ca niciodată. Kutools pentru Excel oferă peste 300 de funcții avansate pentru a crește productivitatea și a economisi timp.  Faceți clic aici pentru a obține funcția de care aveți cea mai mare nevoie...

Descriere


Fila Office aduce interfața cu file în Office și vă face munca mult mai ușoară

  • Activați editarea și citirea cu file în Word, Excel, PowerPoint, Publisher, Access, Visio și Project.
  • Deschideți și creați mai multe documente în filele noi ale aceleiași ferestre, mai degrabă decât în ​​ferestrele noi.
  • Vă crește productivitatea cu 50% și reduce sute de clicuri de mouse pentru dvs. în fiecare zi!
Comments (28)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I want every tab to turn Red if G18 is greater than 100. Could someone help me write this code?
This comment was minimized by the moderator on the site
Hello, Lesa,

To modify the code so that when the value in G18 exceeds 100, the color of all worksheet tabs is set to red, you need to implement this functionality in the ThisWorkbook event, not in the Worksheet_Change event of a single worksheet.
1. Open the VBA editor (Alt + F11).
2. In the Project Explorer, find the ThisWorkbook object and double-click to open it.
3. Copy and paste the following code into the code window of ThisWorkbook:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Not Intersect(Target, Sh.Range("G18")) Is Nothing Then
        Dim ws As Worksheet
        If Sh.Range("G18").Value > 100 Then
            For Each ws In ThisWorkbook.Worksheets
                ws.Tab.Color = RGB(255, 0, 0)
            Next ws
        Else
            For Each ws In ThisWorkbook.Worksheets
                ws.Tab.ColorIndex = xlColorIndexNone
            Next ws
        End If
    End If
End Sub


Result:
If G18 > 100 in any sheet, it iterates through all the worksheets in the workbook, setting their tab color to red.
If the condition is not met, it resets the tab color of all worksheets to the default color.

Please have a try!
This comment was minimized by the moderator on the site
Hi,


I used this code:

Option Explicit
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'Updateby Extendoffice 20160930
Select Case Sheets("Master").Range("A1").Value
Case "KTE"
Sheets("Sheet1").Tab.Color = vbRed
Case "KTO"
Sheets("Sheet2").Tab.Color = vbGreen
Case "KTW"
Sheets("Sheet3").Tab.Color = vbBlue
End Select
End Sub

The True, False or Else function works for me when I type it manually. However, I have a formula that displays True, False or Else as the result. This formula suddenly causes no response when changing the tab color. The formula is based on another sheet, is this a problem?
This comment was minimized by the moderator on the site
Hello Fabian,

Same problem here, have you found any solution to solve this problem ?

Thanks.
This comment was minimized by the moderator on the site
I have a workbook that has 350+ tabs in it. I would like to change the color of each tab based on information in cell G3 on that individual tab. For example, if cell G3 on sheet 1 = 'Water Production' change the tab to green. Is there a way to do this for every tab without having to each tab individually?
This comment was minimized by the moderator on the site
I want to set the sheet tab color based on the color of cell j4 in each sheet. There are 18+ Tabs and want to update the tab colors when the workbook is opened. If I can’t update when opened I can run the macro after the teams update the sheets.
This comment was minimized by the moderator on the site
Hello, wood
To solve your problem, please apply the below code: (Note: Copy and paste the below code into the ThisWorkbook code mode)
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
Call SetSheetColor
End Sub

Private Sub Workbook_Open()
Call SetSheetColor
End Sub

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Call SetSheetColor
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Call SetSheetColor
End Sub

Sub SetSheetColor()
Dim xWShs As Sheets
Dim xRg As Range
Dim xFNum As Integer
Dim xSh As Worksheet
On Error Resume Next
Set xWShs = Application.ActiveWorkbook.Sheets
For xFNum = 1 To xWShs.Count
    Set xSh = xWShs.Item(xFNum)
    Set xRg = xSh.Range("J4")
    xSh.Tab.Color = xRg.Interior.Color
Next
End Sub


https://www.extendoffice.com/images/stories/comments/comment-skyyang/doc-color-sheet.png

Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
I would like my tab to change color if any cell in column O or column P has a value in it. Is this possible?

Thank you!
This comment was minimized by the moderator on the site
Hello ,
I require some help
I need to change the sheet tab color only if in a certain range of cells, has today's date
Say for example,
Column L has some dates in the format (13-May-22)
One of the cell value is today's date, then the tab sheet color should change to red
Please help
Thank you in advance
This comment was minimized by the moderator on the site
Im looking to change to color of the tab based off one of 2 outcomes of a formula I have a formula of=IF((AND(AA1="Occupied",AA2="Occupied",AA3="Occupied",AA4="Occupied")),"Occupied","Vacant")
I need the tab to be red if "Occupied" and green if "Vacant". However the code above in the main post doesn't recognize the output of the If Than
This comment was minimized by the moderator on the site
Hello, Trying to have tab colour change based on value on a seperate sheet called Tracking here is what I tried but it does not seem to be working. Thanks




Private Sub Worksheet_Change(ByVal Target As Range)

'Updateby Extendoffice 20160930

If Target.Address = "Tracking!$C$2" Then

Select Case Target.Value

Case "ip"

Me.Tab.Color = vbRed

Case "w"

Me.Tab.Color = vbYellow

Case "c"

Me.Tab.Color = vbGreen

Case Else

Me.Tab.Color = vbBlue

End Select

End If

End Sub
This comment was minimized by the moderator on the site
Hello, Brad,
To make the code run correctly, you should not insert the sheet name into the code, please apple the following code:(click the sheet name you want to run this code, and then right clik the sheet name, and choose View Code, then paste the code into the module)

Private Sub Worksheet_Change(ByVal Target As Range)

'Updateby Extendoffice 20160930

If Target.Address = "$C$2" Then

Select Case Target.Value

Case "ip"

Me.Tab.Color = vbRed

Case "w"

Me.Tab.Color = vbYellow

Case "c"

Me.Tab.Color = vbGreen

Case Else

Me.Tab.Color = vbBlue

End Select

End If

End Sub

Please try, hope it can help you!
This comment was minimized by the moderator on the site
Hello,

Never done code in excel before. I need a code that do what this code do, but I need another parameter.

So what I need is:

If cell value is not 0, and/or if some other cell has numbers in it, using count, then change color to red

If cell value is 0 and those other cells are empty, using count, then change color to green
This comment was minimized by the moderator on the site
How would you link the tab to a value in a different tab. In my example I have one tab with all the information that filters out to various tabs automatically. However each tab will read green or red, depending on whether there is a bAlance outstanding on the master tab. Can this be done using this code and if so, where do I write the link to the master sheet within this code?
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations