IF语句在VBA中更改不正确的非数字值的格式

我有这个代码:

With .Cells(i, 6) If .NumberFormat <> "0.0%" Then .NumberFormat = "0.0%" If .Value2 <> vbNullString And IsNumeric(.Value2) Then .Value = .Value / 100 If .Value2 = vbNullString Then .Value = "---" .HorizontalAlignment = xlRight End If If .Value >= 0.9 Then .Interior.Color = RGB(237, 67, 55) .Font.Color = vbWhite End If Else .Value = 0 End If End With 

代码所做的是在列中查找超过90%的值,如果是这样,则将单元格内部格式化为红色,将字体格式化为白色。 但我有同一列没有任何价值的一些细胞,因此我希望细胞有这个“—”在里面,使它看起来整洁,但是当我运行的代码单元格“— “在它被合成为红色填充和白色字体以及。 我想要的是那些细胞保持原来的格式。

我已经写了这个if语句,但不知道在“THEN”部分之后要写什么:

 IF .Value = "---" Then 

我是菜鸟! 谢谢你的帮助!

这会回答吗?

  With .Cells(i, 6) If .NumberFormat <> "0.0%" Then .NumberFormat = "0.0%" If .Value <> vbNullString And IsNumeric(.Value2) Then .Value = .Value / 100 if .Value >= 0.9 Then .Interior.Color = RGB(237, 67, 55) .Font.Color = vbWhite End If elseIf .Value = vbNullString Then .Value = "---" .HorizontalAlignment = xlRight Else stop 'I think you forgot this case end if Else .Value = 0 End If End With 

编辑:我添加@Zac的build议:对红色使用条件格式。

优先考虑你的testing,这应该做的伎俩:

 With .Cells(i, 6) If .NumberFormat <> "0.0%" Then .NumberFormat = "0.0%" If .Value2 <> vbNullString Then If IsNumeric(.Value2) Then .Value2 = .Value2 / 100 If .Value2 >= 0.9 And .Value <> "---" Then .Interior.Color = RGB(237, 67, 55) .Font.Color = vbWhite End If Else .Value = "---" .HorizontalAlignment = xlRight End If Else .Value = 0 End If End With