我正在尝试创建一个按钮,只需单击一下,就会检查10张工作簿中的特定单元格,并根据单元格值重新着色选项卡。
例如,
只需单击一下按钮,即可评估所有10个标签并重新着色。
到目前为止,我的宏看起来像这样,只给出了三张示例。这很粗糙,但我对VBA很新(1天)。
我的主要问题是它适用于第一个标签,但随后会打开第二个标签并显示“需要对象”
Sub Update_Tab_Colour_One_Click()
Sheets(4).Activate
If Cells(13, 11).Value > 18 Then
With ActiveWorkbook.ActiveSheet.Tab
.Color = vbGreen
End With
Else
With ActiveWorbook.ActiveSheet.Tab
.Color = vbRed
End With
End If
Sheets(5).Activate
If Cells(13, 11).Value > 18 Then
With ActiveWorkbook.ActiveSheet.Tab
.Color = vbGreen
End With
Else
With ActiveWorbook.ActiveSheet.Tab
.Color = vbRed
End With
End If
Sheets(6).Activate
If Cells(13, 11).Value > 18 Then
With ActiveWorkbook.ActiveSheet.Tab
.Color = vbGreen
End With
Else
With ActiveWorbook.ActiveSheet.Tab
.Color = vbRed
End With
End If
End Sub
答案 0 :(得分:4)
看看这是否适合你:
Sub Update_Tab_Colour_One_Click()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Index = 4 Or ws.Index = 5 Or ws.Index = 6 Then
If ws.Cells(13, 11).Value > 18 Then
ws.Tab.Color = vbGreen
Else
ws.Tab.Color = vbRed
End If
End If
Next ws
End Sub
检查它是4
,5
还是6th
索引表,然后检查单元格值并相应地为标签着色。
答案 1 :(得分:3)
这样的事可能会这样做。
$ docker run -d -e VIRTUAL_HOST=test.example.com \
-v "$PWD/your/php/code/dir":/var/www/html php:7.0-apache
这里我们在索引4:13的表格上创建了一个Dim sh as Worksheet
Dim s as Long
For s = 4 to 13 ' Modify if needed
Set sh = ThisWorkbook.Worksheets(s)
With sh
.Tab.Color = IIF(.Cells(13,11).Value > 18, vbGreen, vbRed)
End With
Next
循环(10张,递增1)。然后,我们设置For/Next
变量(Worksheet
)来表示当前工作表(请注意,它不需要sh
),然后根据Active
设置sh.Tab.Color
IIF
函数中的布尔表达式返回vbGreen
或vbRed
。
的信息:
For..Next
声明参考
IIF
功能参考
答案 2 :(得分:1)
此备选方案允许您循环不连续的纸张(因此2,4,7不仅仅是1,2,3)和它们的名称(如“Sheet1”,“Sheet2”)。所以它更灵活!
它恰好与直接循环一样短,我们只是循环遍历一组工作表名称或数字。
我添加了评论来解释每行的作用,见下文:
Sub Update_Tab_Colour_One_Click()
' Declare array of sheet numbers and/or names
Dim mySheets As Variant
mySheets = Array(2, 4, "Sheet1")
' Loop over sheet numbers / names
Dim i As Long
For i = LBound(mySheets) To UBound(mySheets)
' Use With so we don't have to repeatedly say we are within this sheet
With ThisWorkbook.Sheets(mySheets(i))
' Use IIF to concisely assign a conditional value
.Tab.Color = IIf(.Cells(13, 11).Value > 18, vbGreen, vbRed)
End With
Next i
End Sub