excel - 每次刷新页面时循环更改特定单元格中字体颜色的范围
问题描述
我已经编写了这段代码作为开始。我认为我走在正确的道路上,但代码显然需要更多才能工作。基本上我想从单元格 W8 开始:
- 如果字体在 W8 中是黑色的,它应该移动到 W9 并通过范围(W8、W9、W10、W11、W12、Y8、Y9、Y10、Y11、Y12,然后作为循环返回到 W8)检查字体是否是黑色的。
- 如果循环在字体为红色的单元格处停止,则应将其更改为黑色,并且列表中的下一个单元格应更改为红色。然后它应该退出子程序。
SubFontW8()
If Range("W8").Font.Color = vbRed Then
Range("W8").Font.Color = xlAutomatic
And
Range("W9").Font.Color = vbRed
Exit Sub
Else
Call SubFontW9()
End If
End Sub
SubFontW9()
If Range("W9").Font.Color = vbRed Then
Range("W9").Font.Color = xlAutomatic
And
Range("W10").Font.Color = vbRed
Exit Sub
Else
Call SubFontW10()
End If
End Sub
SubFontW10()
If Range("W10").Font.Color = vbRed Then
Range("W10").Font.Color = xlAutomatic
And
Range("W11").Font.Color = vbRed
Exit Sub
Else
Call SubFontW11()
End If
End Sub
SubFontW11()
If Range("W11").Font.Color = vbRed Then
Range("W11").Font.Color = xlAutomatic
And
Range("W12").Font.Color = vbRed
Exit Sub
Else
Call SubFontW12()
End If
End Sub
SubFontW12()
If Range("W12").Font.Color = vbRed Then
Range("W12").Font.Color = xlAutomatic
And
Range("Y8").Font.Color = vbRed
Exit Sub
Else
Call SubFontY8()
End If
End Sub
SubFontY8()
If Range("Y8").Font.Color = vbRed Then
Range("Y8").Font.Color = xlAutomatic
And
Range("Y9").Font.Color = vbRed
Exit Sub
Else
Call SubFontY9()
End If
End Sub
SubFontY9()
If Range("Y9").Font.Color = vbRed Then
Range("Y9").Font.Color = xlAutomatic
And
Range("Y10").Font.Color = vbRed
Exit Sub
Else
Call SubFontY10()
End If
End Sub
SubFontY10()
If Range("Y10").Font.Color = vbRed Then
Range("Y10").Font.Color = xlAutomatic
And
Range("Y11").Font.Color = vbRed
Exit Sub
Else
Call SubFontY11()
End If
End Sub
SubFontY11()
If Range("Y11").Font.Color = vbRed Then
Range("Y11").Font.Color = xlAutomatic
And
Range("Y12").Font.Color = vbRed
Exit Sub
Else
Call SubFontY12()
End If
End Sub
SubFontY12()
If Range("Y12").Font.Color = vbRed Then
Range("Y12").Font.Color = xlAutomatic
And
Range("W8").Font.Color = vbRed
Exit Sub
Else
Call SubFontW8()
End If
End Sub
您的任何帮助将不胜感激。谢谢你。
解决方案
有几种方法可以完成此任务。首先,您将需要一个For Next Loop。在此上下文中,您可以使用 aFor Next
或 a For Each
。我建议阅读更多关于循环的内容以更好地理解。
为简单起见,我将使用For Each
循环。
请参见下面的代码(插入到 VBE 中的任何普通模块中):
Option Explicit
Sub Change_Font_Colour()
'range where you want to change the font colours
Dim rOperationRange As Range
'variable used to loop through the operation range
Dim cell As Range
'set range object
'you should ALWAYS have a worksheet qualifier
'change the sheet name to the required sheet name and adjust your range accordingly
Set rOperationRange = ThisWorkbook.Sheets("mySheetName").Range("W8:Y12")
'for loop
For Each cell In rOperationRange
If cell.Font.Color = vbRed Then
'set to black
cell.Font.Color = vbBlack
'set cell BELOW as red using .Offset
cell.Offset(1, 0).Font.Color = vbRed
'now exit
Exit Sub
End If
Next cell
End Sub
推荐阅读
- javascript - 尝试获取事件侦听器的目标时出现“未定义目标”错误
- java - Java视频聊天应用程序输入输出流不起作用
- asp.net - 如何使用 Open XML 设置 Excel 单元格样式
- magento - 特定国家/地区的 COD 额外运费 Magento 2
- typescript - 找不到名称“IProp”。带有 TS 3.3.3 命名空间的 VS Code 中的 ts(2304)
- vb.net - 如何从 vb.net 的数据库中填充 datagridview 中的组合框并在组合框中加载默认的第一个单元或项目
- windows - 按端口号的 Windows 终止进程
- c# - 如何使用事件扩展类
- flutter - Flutter:根据可见性构建子级
- android - 如何启用 StrongBox(28 及以上版本可用)?