如果语句更改VBA中不正确的非数值的格式
我有这个密码:如果语句更改VBA中不正确的非数值的格式,vba,excel,Vba,Excel,我有这个密码: 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
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
我是新手!谢谢你的帮助 这个答案是什么
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
带.Cells(i,6)
如果.NumberFormat“0.0%”,则
.NumberFormat=“0.0%”
如果.Value vbNullString和IsNumeric(.Value2),则
.Value=.Value/100
如果.Value>=0.9,则
.Interior.Color=RGB(237,67,55)
.Font.Color=vbWhite
如果结束
elseIf.Value=vbNullString然后
.Value=“--”
.HorizontalAlignment=xlRight
其他的
别说了,我想你忘了这个案子
如果结束
其他的
.Value=0
如果结束
以
编辑:我添加了@Zac的建议:对红色使用条件格式 这个答案是什么
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
带.Cells(i,6)
如果.NumberFormat“0.0%”,则
.NumberFormat=“0.0%”
如果.Value vbNullString和IsNumeric(.Value2),则
.Value=.Value/100
如果.Value>=0.9,则
.Interior.Color=RGB(237,67,55)
.Font.Color=vbWhite
如果结束
elseIf.Value=vbNullString然后
.Value=“--”
.HorizontalAlignment=xlRight
其他的
别说了,我想你忘了这个案子
如果结束
其他的
.Value=0
如果结束
以
编辑:我添加了@Zac的建议:对红色使用条件格式 对测试进行优先级排序,这应该可以做到:
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
带.Cells(i,6)
如果.NumberFormat“0.0%”,则
.NumberFormat=“0.0%”
如果.Value2为空字符串,则
如果是数值(.Value2),则.Value2=.Value2/100
如果.Value2>=0.9且.Value为“---”,则
.Interior.Color=RGB(237,67,55)
.Font.Color=vbWhite
如果结束
其他的
.Value=“--”
.HorizontalAlignment=xlRight
如果结束
其他的
.Value=0
如果结束
以
确定测试的优先级,这应该可以做到:
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
带.Cells(i,6)
如果.NumberFormat“0.0%”,则
.NumberFormat=“0.0%”
如果.Value2为空字符串,则
如果是数值(.Value2),则.Value2=.Value2/100
如果.Value2>=0.9且.Value为“---”,则
.Interior.Color=RGB(237,67,55)
.Font.Color=vbWhite
如果结束
其他的
.Value=“--”
.HorizontalAlignment=xlRight
如果结束
其他的
.Value=0
如果结束
以
与其编写代码来实现这一点,不如看看Excel中的“条件格式”。它是用来做这类事情的。在将单元格格式更改为“0.0%”之前,原始单元格格式是什么?@Zac:您可以将空值设置为“---”,但将“”设置为“---”似乎不可能me@Pierre,我想你做不到。我将使用VBA填充单元格值(如果需要),并让条件格式设置
执行实际的单元格格式设置。。。在我的工作中有更好的方法view@Zac你在这一点上确实是对的。添加到我的答案中以获得可见性;-)与其编写代码来实现这一点,不如看看Excel中的“条件格式”。它是用来做这类事情的。在将单元格格式更改为“0.0%”之前,原始单元格格式是什么?@Zac:您可以将空值设置为“---”,但将“”设置为“---”似乎不可能me@Pierre,我想你做不到。我将使用VBA填充单元格值(如果需要),并让条件格式设置
执行实际的单元格格式设置。。。在我的工作中有更好的方法view@Zac你在这一点上确实是对的。添加到我的答案中以获得可见性;-)