简体   繁体   English

如果isblank vba高亮显示单元格

[英]highlight cell if isblank vba

Hi i have the following code but it prommpts an error range of object_worksheet failed. 嗨,我有以下代码,但它提示错误的object_worksheet范围失败。 I'm not sure what i'm doing wrong (i've found the vba code using record macro and simply copy and pasted except i've replaced all of selection to ws.range(emptyrow) to indicate the range is up to the last cell with values. Also, if i were to change the sub to sub highlightemptycell_change() and have if statement as such: "if any cells are changed then do the following" how would i write that in a vba language? 我不确定我在做什么错(我已经使用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

I'm not sure exactly what you are doing. 我不确定您在做什么。 In particular, I'm not sure of the significance of this line, 特别是,我不确定这条线的意义,

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

especially when there are no conditional formats in the cell at the time it is executed. 尤其是当单元执行时没有条件格式时。

But the following macro seems to do what yours would do if it were cleaned up a bit and written with proper syntax 但是下面的宏似乎可以完成您的工作,如果将其清理干净并使用正确的语法编写

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