2013-05-18 59 views
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 
+2

您的意思是使用和? – tangrs

+0

查询“德摩根法则”。 (这里是维基百科的文章,尽管它有点不透明:http://en.wikipedia.org/wiki/De_Morgan's_laws) – RBarryYoung

回答

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 
2

这里有一个简单的改变,是不是W-Z到任何单元格的方式 “ - ”:

If Cells(l, c).Value Like "[!w-zW-Z]" Then 
    Cells(l, c).Value = "-" 
End If 
1

另一种有效的替代方案:

Select Case UCase$(Cells(l, c)) 
    Case "W" To "Z" 
     Cells(l, c).value = "-" 
End Select