VBA - 复制模板工作表并使用用户输入的文本重命名并修改选项卡颜色

时间:2017-05-08 10:23:41

标签: excel vba

我是VBA的新手,我想(1)复制模板,(2)在指定的工作表之前将其放置,以及(3)修改其颜色。

(1)和(2)已经很好,但我在修改颜色方面遇到了问题(这是代码的最后3行),我可以寻求你的帮助来看看......?在此先感谢您的帮助,非常感谢!

Sub Add_ITS()

Dim wb As Workbook: Set wb = ThisWorkbook
Dim wt As Worksheet: Set wt = wb.Sheets("Data Input-ITS template")
Dim ws As Worksheet: Set ws = wb.Sheets("ITSEnd")
Dim newws As Worksheet, sh As Worksheet, newname
Dim query As Long, xst As Boolean, info As String

xst = False
newname = Application.InputBox("Please Enter the name of the Worksheet")
If newname = "False" Then Exit Sub
For Each sh In wb.Sheets
    If sh.Name = newname Then
        xst = True: Exit For
    End If
Next
If Len(newname) = 0 Or xst = True Then
    info = "Sheet name is invalid. Please retry."
    GoTo retry
End If
wt.Copy before:=ws: Set newws = ActiveSheet: newws.Name = newname

With wb.newws.Tab
.Color = 6299648
.TintAndShade = 0

End With
End Sub

2 个答案:

答案 0 :(得分:0)

如果您想使用.Color属性,那么您应该使用vbRedvbBlue等等或RGB值(如下)。

如果要使用数值格式的值,请尝试属性.ColorIndex(值介于0-56之间)或RGB(整数0-255 [红色],整数0-255 [绿色],整数0-255 [蓝色])

例如:

.ColorIndex=23
.Color = RGB(111, 233, 233)

答案 1 :(得分:0)

当你设置Workbook的对象变量时,你不需要指定newws,因为你设置了ActiveWorkbook newws作为ActiveSheet)。尝试删除wb

With newws.Tab
    .Color = 6299648
    .TintAndShade = 0
End With