英文:
Excel VBA Selecting Multiple columns with last row and apply Condition Format
问题
请问需要翻译哪一部分?
英文:
Can someone help me on my below query. I'm trying to Select Multiple columns with last row and applying Condition Format but it is not working in the way I'm trying.
Working One:
Dim lr As Long
With Range("E:E,F:F,H:H")
.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=0"
.FormatConditions(.FormatConditions.Count).SetFirstPriority
.FormatConditions(1).Interior.Color = 13561798
.FormatConditions(1).StopIfTrue = False
End With
This one is not working :
Dim lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
With Range(""E2:E" & lr,"F2:F" & lr,"H2:H" & lr)
.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=0"
.FormatConditions(.FormatConditions.Count).SetFirstPriority
.FormatConditions(1).Interior.Color = 13561798
.FormatConditions(1).StopIfTrue = False
End With
And I'm trying to apply color to a cell that contains error "#N/A" but it is not working.
Dim lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
With Range(""E2:E" & lr,"F2:F" & lr,"H2:H" & lr)
.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=#N/A"
.FormatConditions(.FormatConditions.Count).SetFirstPriority
.FormatConditions(1).Interior.Color = 13561798
.FormatConditions(1).StopIfTrue = False
End With
Any help one above 2 queries would be appreciated..
答案1
得分: 1
FormatConditions.Add
返回刚刚添加的条件,所以你可以在一个With
块中使用它。此外,修复了你的Range()
调用。
对于#N/A:
With .FormatConditions.Add(Type:=xlExpression, Formula1:="=ISNA(E2)")
英文:
Try this:
Sub Test()
Dim lr As Long, ws As Worksheet
Set ws = ActiveSheet 'for example
lr = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
With ws.Range("E2:E" & lr & ",F2:F" & lr & ",H2:H" & lr)
With .FormatConditions.Add(Type:=xlCellValue, _
Operator:=xlEqual, Formula1:="=0")
.SetFirstPriority
.Interior.Color = 13561798
.StopIfTrue = False
End With
End With
End Sub
FormatConditions.Add
returns the just-added condition, so you can use that in a With
block. Also fixed your Range()
call.
For #N/A
With .FormatConditions.Add(Type:=xlExpression, Formula1:="=isNA(E2)")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论