0

我试图让 OR 循环在 VBA 中工作,但不是避免其中包含字母的单元格,而是使整个数组为“-”。即使我输入数字或单词,它也会用破折号替换整个数组。

Private Sub CommandButton1_Click()

Dim l As Double, c As Double

For l = 14 To 18
    For c = 10 To 12


    If Cells(l, c).Value <> "W" Or _
    Cells(l, c).Value <> "w" Or _
    Cells(l, c).Value <> "X" Or _
    Cells(l, c).Value <> "x" Or _
    Cells(l, c).Value <> "Y" Or _
    Cells(l, c).Value <> "y" Or _
    Cells(l, c).Value <> "Z" Or _
    Cells(l, c).Value <> "z" Then
    Cells(l, c).Value = "-"
    End If

    Next c
Next l

End Sub
4

3 回答 3

2

您需要将条件结合在一起。

If Cells(l, c).Value <> "W" And _
    Cells(l, c).Value <> "w" And _
    Cells(l, c).Value <> "X" And _
    Cells(l, c).Value <> "x" And _
    Cells(l, c).Value <> "Y" And _
    Cells(l, c).Value <> "y" And _
    Cells(l, c).Value <> "Z" And _
    Cells(l, c).Value <> "z" Then
于 2013-05-18T13:53:42.253 回答
2

这是将任何不是 wZ 的单元格更改为“-”的更简单方法:

If Cells(l, c).Value Like "[!w-zW-Z]" Then
    Cells(l, c).Value = "-"
End If
于 2013-05-18T13:55:43.187 回答
1

另一个有效的选择:

Select Case UCase$(Cells(l, c))
    Case "W" To "Z"
        Cells(l, c).value = "-"
End Select
于 2013-05-18T15:57:48.507 回答