excel 如何根据工作表上的单元格值更改选项卡的颜色

mnowg1ta  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(195)

我在练习册里有几张纸。每个工作表在单元格U2中有一个日期。我想有一个在U2的工作日值的所有单元格有一个绿色的标签颜色,并在U2的周末值的工作表的标签是黄色的。我已经找到了如何改变标签的颜色,但不知道如何具体告诉它“Sheet4”标签颜色绿色。我正在寻找的代码看起来像:

For each sht in Thisworkbook.worksheets
  If format(sht.Renge("U2"),"DDD") = "Saturday" _
    or format(sht.Renge("U2"),"DDD") = "Sunday" then
    sht.Tab.ColorIndex = "yellow"
  else
    sht.Tab.ColorIndex = "blue"
  end if
Next

下面是我使用的代码:

Sub sbColorAllSheetTab()
    'Declaration
    Dim iCntr, sht As Worksheet

On Error GoTo ErrorHandler

    'Holds the colorIndex number
    iCntr = 2

    'looping throgh the all the sheets of the workbook
    For Each sht In ThisWorkbook.Worksheets

        'Debug.Print Format(sht.Range("U2"), "DDD")  'Tried to check value on sheet - failed
        iCntr = iCntr + 1

        'Applying the colors to Sheet tabs - works
        sht.Tab.ColorIndex = 10 'iCntr

        'Tried to print the value, but didn't work'
        'If I can confirm it sees the correct value in the sheet I can interrogate the value
        'Debug.Print sht.Name  '.Range("U2")
        Debug.Print sht.Range("U2") 'Failed
    Next

   Exit Sub
ErrorHandler:
   ' Error handling code
   Beep
   Resume Next
End Sub

谢谢

eyh26e7m

eyh26e7m1#

根据您的请求(“我正在寻找代码看起来像:”)的精神,您的代码非常接近。
我只改变了:
1.您的日期格式(从“DDD”到“DDDD”)

  1. Range的拼写错误
    1.用整数代替制表符颜色代替字符串
    最接近你的代码,我得到的工作是:
For Each sht In ThisWorkbook.Worksheets
  If Format(sht.Range("U2"), "DDDD") = "Saturday" _
    Or Format(sht.Range("U2"), "DDDD") = "Sunday" Then
    sht.Tab.ColorIndex = 6 'yellow
  Else
    sht.Tab.ColorIndex = 5 'blue
  End If
Next

相关问题