繁体   English   中英

如果isblank vba高亮显示单元格

[英]highlight cell if isblank vba

嗨,我有以下代码,但它提示错误的object_worksheet范围失败。 我不确定我在做什么错(我已经使用record宏找到了vba代码并只是复制并粘贴了,除了我已经将所有selection都替换为ws.range(emptyrow)来指示范围取决于另外,如果我将sub更改为sub highlightemptycell_change()并具有if语句,例如:“如果更改了任何单元格,则执行以下操作”我将如何用vba语言编写?

sub highlightemptycell()
    Dim ws As Worksheet
    Dim r As Range
    Dim emptyrow As Long
    Dim err As Range

    Set ws = Worksheets("Master")
    emptyrow = ws.Cells(Rows.Count, 1).End(xlUp).Row + 1    '<<< safer....

    ws.Range(emptyrow).FormatConditions(1).StopIfTrue = False
    ws.Range(emptyrow).FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=ISBLANK(ws.range(emptyrow)"
    ws.Range(emptyrow).FormatConditions(ws.Range(emptyrow).FormatConditions.Count).SetFirstPriority
    With ws.Range(emptyrow).FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 5287936
        .TintAndShade = 0
    End With

我不确定您在做什么。 特别是,我不确定这条线的意义,

ws.Range(emptyrow).FormatConditions(1).StopIfTrue = False   

尤其是当单元执行时没有条件格式时。

但是下面的宏似乎可以完成您的工作,如果将其清理干净并使用正确的语法编写

Option Explicit
Sub highlightemptycell()
    Dim ws As Worksheet
    Dim r As Range
    Dim emptyrow As Long
    Dim err As Range

    Dim rEmptyRow As Range '<-- range object added to use below

    Set ws = Worksheets("Master")
    Set rEmptyRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(rowoffset:=1)    '<<< safer....

With rEmptyRow.FormatConditions
    If .Count > 0 Then .Item(1).StopIfTrue = False
    .Add Type:=xlExpression, Formula1:= _
        "=ISBLANK(" & rEmptyRow.Address & ")"
        .Item(.Count).SetFirstPriority
    With .Item(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 5287936
        .TintAndShade = 0
    End With
End With
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM