2013-05-18 106 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