Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1326|回复: 4

[已解决]VBA设置条件格式

[复制链接]
发表于 2022-8-26 14:25 | 显示全部楼层 |阅读模式
本帖最后由 chensir 于 2022-8-26 14:28 编辑

请教各位,如何用语句实现设置图一里的条件格式,就是B:D里,如果B2<>B1,单元格下框线变为红色

最佳答案
2022-8-26 15:02
  1. Sub Macro1() '录制宏
  2.     With Selection.Borders(xlEdgeTop)
  3.         .Weight = xlThin
  4.         .LineStyle = xlContinuous
  5.         .Color = 255
  6.         .TintAndShade = 0
  7.     End With
  8. End Sub
  9. Sub TEST()
  10.     Dim K As Integer
  11.     For K = 2 To Cells(Rows.Count, 2).End(3).Row
  12.         If Cells(K, 2) <> Cells(K - 1, 2) Then
  13.             Cells(K, 2).Resize(1, 4).Select
  14.             Call Macro1
  15.         End If
  16.     Next K
  17. End Sub
复制代码
1661494742547.png
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2022-8-26 15:02 | 显示全部楼层    本楼为最佳答案   
  1. Sub Macro1() '录制宏
  2.     With Selection.Borders(xlEdgeTop)
  3.         .Weight = xlThin
  4.         .LineStyle = xlContinuous
  5.         .Color = 255
  6.         .TintAndShade = 0
  7.     End With
  8. End Sub
  9. Sub TEST()
  10.     Dim K As Integer
  11.     For K = 2 To Cells(Rows.Count, 2).End(3).Row
  12.         If Cells(K, 2) <> Cells(K - 1, 2) Then
  13.             Cells(K, 2).Resize(1, 4).Select
  14.             Call Macro1
  15.         End If
  16.     Next K
  17. End Sub
复制代码
回复

使用道具 举报

发表于 2022-8-26 15:20 | 显示全部楼层
本帖最后由 zjdh 于 2022-8-26 15:21 编辑

Sub Macro1()
    Range("B:D").FormatConditions.Add Type:=xlExpression, Formula1:="=R[1]C2<>RC2"
    With Range("B:D").FormatConditions(1).Borders(xlBottom)
        .LineStyle = xlContinuous
        .Color = 255
        .Weight = xlThin
    End With
End Sub
回复

使用道具 举报

发表于 2022-8-26 15:23 | 显示全部楼层
本帖最后由 zjdh 于 2022-8-26 15:29 编辑

2楼不是设置条件格式,而是设置框线,新添加内容就不会自动添加红色间隔线!
回复

使用道具 举报

 楼主| 发表于 2022-8-26 16:01 | 显示全部楼层
zjdh 发表于 2022-8-26 15:23
2楼不是设置条件格式,而是设置框线,新添加内容就不会自动添加红色间隔线!

对的,感谢、感谢。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-3 08:32 , Processed in 0.241294 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表