请教EXCEL宏高手!一个设置条件格式能不能写入宏?

在EXCEL表格中我设置了条件格式,但编辑中这个条件格式会总是自动修改,所以想把它写入宏里。我的设置步骤是:使用条件格式,新建规则——使用公式确定要设置格式的单元格——条件公式=$B1<>INDIRECT("B"&ROW()+1)——同时设置单元格底纹为黄色。
我的表格原来就有宏,下面是代码,请问要怎么加入,最好帮我修改后发上来。因为我不太懂宏。
Private Sub Worksheet_Change(ByVal TargetAs Range)
If Selection.Count = 1 Then
If Target.Column = 2 Then
Application.EnableEvents = False
Else
Application.EnableEvents = False
If Range("B" & Target.Row) = "" Then
Range("B" & Target.Row).NumberFormatLocal ="yyyy/mm/dd"
Range("B" & Target.Row) = Range("B" &Target.Row).Offset(-1, 0)
End If
If Range("F" & Target.Row).Formula = "" Then
Range("F" & Target.Row) = "=IF(OR($B" &Target.Row & "="""",$B" & Target.Row& "=OFFSET($B" & Target.Row & ",1,)" &"),"""",SUMIF($B:$B,$B" & Target.Row &",$E:$E))"
End If
If Range("I" & Target.Row).Formula = "" Then
Range("I" & Target.Row) = "=E" & Target.Row& "-H" & Target.Row
End If
End If
Application.EnableEvents = True
End If
End Sub
还有写入宏后, 表格中的设置的条件格式是不是就不会改变了。

第1个回答  2014-07-17
Private Sub Worksheet_Change(ByVal TargetAs Range)
If Selection.Count = 1 Then
   If Target.Column = 2 Then
       Application.EnableEvents = False
   Else
       Application.EnableEvents = False
       If Range("B" & Target.Row) = "" Then
           Range("B" & Target.Row).NumberFormatLocal ="yyyy/mm/dd"
           Range("B" & Target.Row) = Range("B" &Target.Row).Offset(-1, 0)
       End If
       If Range("F" & Target.Row).Formula = "" Then
           Range("F" & Target.Row) = "=IF(OR($B" &Target.Row & "="""",$B" & Target.Row& "=OFFSET($B" & Target.Row & ",1,)" &"),"""",SUMIF($B:$B,$B" & Target.Row &",$E:$E))"
       End If
       If Range("I" & Target.Row).Formula = "" Then
           Range("I" & Target.Row) = "=E" & Target.Row& "-H" & Target.Row
       End If
  if  cells(target.row+1,"B") > [B1] then target.Interior.color=vbyellow  '加一句就好了
 End If
   Application.EnableEvents = True
End If
End Sub

追问

没用哦,你写的这个是什么意思?是我上面设置条件格式的过程吗?为什么没用呢?

追答

你要对B列整列设么,
这个只是让 当前单元格变色 (如果当前单元格的下一格>B1)

追问

不对,你看公式应该就知道啊。

追答

看了半边,不等于
改为
if cells(target.row+1,"B") [B1] then target.Interior.color=vbyellow '加一句就好了

追问

还是没用,我的这个设置的意思是每天日期的最后一行自动填充为黄色底纹。设置的单元格区域为B列到I列。

相似回答