非大小写敏感格式(Non Case Sensitive Formatting)

我有一个excel表,其中包含一个列,其中包含“病态”等值,我想在按下按钮时使用删除线格式化。 如果单元格值为“Sick”,我有以下代码可以正常工作,但如果值“生病”则没有,有没有办法我可以更改代码使其不区分大小写?

For Each rng In ws.Range("E1:E" & lastrow) If rng.Value = "Sick" Then ws.Range("A" & rng.Row).Resize(1, 2).Font.Strikethrough = True End If Next rng

I have an excel sheet that has a column that contains values such as "sick" that I want to format with strikethroughs when a button is pressed. I have the following code that works perfect if the cell value is "Sick" but not if the value is "sick" is there a way I can change the code to make it not case sensitive?

For Each rng In ws.Range("E1:E" & lastrow) If rng.Value = "Sick" Then ws.Range("A" & rng.Row).Resize(1, 2).Font.Strikethrough = True End If Next rng

最满意答案

将行更改为:

If LCase(rng.Value) = "sick" Then

这将查看范围值的小写版本,并根据小写字符串进行检查。 这确保了无论范围的值如何大写,小写将始终与小写进行比较。

顺便说一下,VBA还支持UCase()函数以及转换为全部大写,因此您的问题也可以通过以下方式解决:

If UCase(rng.Value) = "SICK" Then

Change the line to:

If LCase(rng.Value) = "sick" Then

Which will look at the lower case version of the range's value and check it against the lower case string. This ensures that no matter how the range's value is capitalized, lower case will always be compared against lower case.

By the way, VBA also supports a UCase() function as well for converting to all upper-case, so your problem could also be solved with:

If UCase(rng.Value) = "SICK" Then

更多推荐