【发布时间】:2019-02-03 03:48:17
【问题描述】:
我想编写一个代码,根据单元格值更改选定单元格范围的样式属性。
当我只更改文本颜色或字体时它起作用了,但是如果代码什么都不做,我会为每个参数添加多个参数。我也没有收到错误。
Dim userRange As Range
Set userRange = Application.InputBox("Select a range", Type:=8)
For Each cell In Selection
If cell.Value < 0 Then cell.Font.FontStyle = "Comic Sans MS" & cell.Font.Size = 18 & cell.Font.Color = vbRed
If cell.Value >= 0 And cell.Value <= 500 Then cell.Font.Bold = True & cell.Font.Italic = True & cell.Font.Underline = True
If cell.Value > 500 And cell.Value <= 1000 Then cell.Font.FontStyle = "Monotype Corsiva" & cell.Font.Color = vbBlue & cell.Font.Underline = xlUnderlineStyleDouble
If cell.Value > 1000 Then cell.Font.FontStyle = "Arial" & cell.Font.Bold = True & cell.Font.Italic = True & cell.Interior.Color = vbGreen & cell.Font.Color = vbWhite
Next cell
我想我真的很接近,但我似乎无法弄清楚我做错了什么!我希望我的解释很清楚,因为我真的不习惯编程/编写脚本。
提前致谢!
【问题讨论】:
-
如果你真的想在一个代码行中添加连续的赋值,你必须使用
:字符作为行分隔符(但是,这不是 logical AND 替换) 而不是与符号&。否则,在If条件中使用单独的代码行更易读。 -
好吧,我想在我写这样的代码之前我需要多学习一点!谢谢你的提示!我已经在想我的代码看起来有点乱了!
-
请注意,如果您需要多次运行并且值可以在运行之间更改,则需要先清除/重置 所有 样式属性...