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

时间:2016-01-08 23:04:17

标签: excel-vba vba excel

我在工作簿中有几张。每个工作表在单元格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

由于

1 个答案:

答案 0 :(得分:1)

本着您的要求("我正在寻找代码:"),您的代码非常接近。

我只改变了:

  1. 您的日期格式(从" DDD"到" DDDD")
  2. Range的拼写错误
  3. 用标签颜色替换整数代替字符串
  4. 我工作的代码最接近的是:

    var dataObject = {phyloxml: tree_data};
    var phylocanvas = new Smits.PhyloCanvas(
      dataObject,
      'svgCanvas',
      1000, 1000,
      'circular'
    );
    var paper = phylocanvas.getSvg().svg;