我想要一个子程序在打开工作簿时执行,对于工作簿中的特定工作表,请执行以下操作。
根据同一列中另一个单元格的值更改单元格中按钮文本的颜色。将有多个按钮来执行此操作,因此每个按钮在不同的单元格中都有相应的值来查询,然后更改按钮的文本颜色。
开始,第12行,B列,遍历到M列,如果value大于0, 我希望同一列中按钮的文本更改为红色。所以在第 14 行,那些从 B 列开始并遍历到 M 列的按钮。
这是我放在工作簿打开中的代码。
我可以很好地遍历第 12 行,现在我如何获得对第 14 行按钮的引用,以便我可以更改按钮文本的颜色?
Private Sub Workbook_Open()
Dim ws As Worksheet
Dim colRange As String
colRange = "B12:M12"
' Set the worksheet name.
Set ws = ThisWorkbook.Sheets("ForIrsIncomeAndExpenses")
With ws
For Each cell In Range(colRange)
' Change row 14's button's text color to red if this cell value is greater than 0.
' Change row 14's button's text color to black if the cell value is not greater than 0.
If cell.Value > 0 Then
' How do I get a reference to the button on row 14 for this column(cell)?
' btn.Font.Color = vbRed
MsgBox ">0"
Else
' How do I get a reference to the button on row 14 for this column(cell)?
' btn.Font.Color = vbBlack
MsgBox "=0"
End If
Next cell
End With
End Sub
这是表格。