根据选项卡名称excel vba更改选项卡颜色

时间:2017-11-03 16:46:57

标签: excel vba excel-vba

我希望能够根据制表符单元格的名称更改制表符颜色。我试图让我的版本从名称列表中选择以更改选项卡名称:

<core:FragmentDefinition xmlns="sap.m"
    xmlns:core="sap.ui.core">
    <Page id="jobTableDisplayPage" showHeader="false" enableScrolling="true">
        <content>
            <Table id="jobTable" items="{myTable>/d/results/}">
                <columns>
                    <Column width="15em">
                        <Text text="Job Name" />
                    </Column>
                    <Column width="5em">
                        <Text text="User Name" />
                    </Column>
                    <Column width="5em">
                        <Text text="Job Status" />
                    </Column>
                    <Column width="5em">
                        <Text text="Start Date" />
                    </Column>
                    <Column width="5em">
                        <Text text="Start Time" />
                    </Column>
                    <Column width="5em">
                        <Text text="End Time" />
                    </Column>
                    <Column width="5em">
                        <Text text="Spool Number" />
                    </Column>
                </columns>
                <items>
                    <ColumnListItem>
                        <cells>
                            <Text text="{myTable>Jobname}" />
                            <Text text="{myTable>Usern}" />
                            <Text text="{myTable>Status}" />
                            <Text text="{myTable>Startdate}" />
                            <Text
                                text="{  path: '{myTable>Starttime}',
                                         type: 'sap.ui.model.type.Time',
                                         formatOptions: {
                                               relative: true,
                                               relativeScale: 'auto'
                                       }
                                     }" />
                            <Text text="{myTable>Endtime}" />
                            <Text text="{myTable>Spool}" />
                        </cells>
                    </ColumnListItem>
                </items>
            </Table>
        </content>
    </Page>
</core:FragmentDefinition>

运行此脚本后,我在&#34;设置mySheets =&#34;线

Error Msg

任何反馈都会受到赞赏,我知道我甚至可能不会接近。

2 个答案:

答案 0 :(得分:4)

  

表格与工作表不同。

     

您正尝试将Sheets分配给Worksheets,因此错误。

使用相同的类型,代码可以正常工作。

Sub test()

    Dim mySheets As Sheets
    Dim mySheet As Worksheet

    Set mySheets = Sheets(Array("Sheet1", "Sheet2", "Sheet3"))
    For Each mySheet In mySheets
        mySheet.Tab.Color = RGB(0, 255, 255)
    Next

 End Sub

答案 1 :(得分:3)

试试这个,它将名字数组作为字符串而不是表格。

Sub SheetTabColor()

    Dim mySheets() As Variant
    Dim mySheet As Variant

    mySheets = Array("Christine", "Marina", "Roberto", "Urszula", "Lois", "Matt", "Stephanie", "Sally", "Iryna", "Katherine", "Matthew", "Julio", "Lavinia")

    For Each mySheet In mySheets
        ThisWorkbook.Worksheets(mySheet).Tab.Color = RGB(0, 255, 255)
    Next

End Sub

如果找不到数组中的任何名称作为工作表名称,则此代码将失败。

以下内容将跳过未找到的内容:

Sub SheetTabColor()

    Dim mySheets() As Variant
    Dim mySheet As Variant

    mySheets = Array("Christine", "Marina", "Roberto", "Urszula", "Lois", "Matt", "Stephanie", "Sally", "Iryna", "Katherine", "Matthew", "Julio", "Lavinia")

    For Each mySheet In mySheets
        On Error Resume Next
            ThisWorkbook.Worksheets(mySheet).Tab.Color = RGB(0, 255, 255)
        On Error GoTo 0
    Next

End Sub