excel - 如何在 FormatConditions.Add 方法的 xlExpression 中使用 LEFT 函数?
问题描述
我想在基于 LEFT(cell,1) 值的范围内设置条件格式。在此范围内,a 的值为“1A”,如果第一个字符 LEFT() 等于 1,我想将其涂成红色。我的代码什么也不做。我怎样才能做到这一点?
我的代码
Sub ConditionalFormatting()
Dim condition1 As FormatCondition, condition2 As FormatCondition
Dim tbl As ListObject
Set tbl = ThisWorkbook.Worksheets("Overview").ListObjects("Column_Overview")
Dim tblLastColumn As Integer
tblLastColumn = tbl.Range.Columns.Count
'Fixing/Setting the range on which conditional formatting is to be desired
Dim rng As Range
Set rng = tbl.ListColumns(8).DataBodyRange.Resize(, tblLastColumn - 7)
'Defining and setting the criteria for each conditional format
Set condition1 = rng.FormatConditions.Add(Left(xlCellValue, 1), xlEqual, "=1")
Set condition2 = rng.FormatConditions.Add(xlCellValue, xlLess, "=50")
'Defining and setting the format to be applied for each condition
With condition1
.Font.Color = vbRed
.Font.Bold = True
End With
解决方案
请尝试这种方式:
Sub testFormatCondLeft()
Dim condition1 As FormatCondition
Dim condition1 As FormatCondition, rng As Range
'your existing code...
Set rng = tbl.ListColumns(8).DataBodyRange.Resize(, tblLastColumn - 7)
With rng
.FormatConditions.Delete
Set condition1 = .FormatConditions.Add(Type:=xlExpression, Formula1:="=Left(" & .cells(1).Address(0, 0) & ", 1)=""1""")
End With
With condition1
.Font.Color = vbRed
.Font.Bold = True
End With
End Sub
推荐阅读
- javascript - Firebase 重置密码链接在屏幕上显示错误
- javascript - SVG 过滤器关闭我的 CSS 过滤器。为什么?
- php - IIS 上的 PHP 页面说没有指定输入文件
- node.js - 无法将 Node.JS 卸载/修复/安装到我的系统
- python - Plotly-Dash:如何提高对滑块变化的绘图响应能力
- ruby-on-rails - 收到“未找到”。身份验证通道。带有命名空间控制器的 Devise omniauthable 错误
- webrtc - 目前在P2P领域,只使用STUN(不使用Turn Server)打孔成功率是多少?
- mule - 如何从 Mule 3 连接 Cloud Kafka 集群?
- regex - 使用正则表达式从 IAM 策略解析 ARN
- c++ - 如何使用 boost::beast 连接到 Coinbase 的 websocket API