ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Excel VBA OR statement (https://www.excelbanter.com/excel-programming/354883-excel-vba-statement.html)

[email protected]

Excel VBA OR statement
 
Why does the OrRstatement for "y" does not work?

Private Sub Worksheet_Change(ByVal Target As Excel.Range)

If Target.Column = 3 Then

If (Target.Text = "Y" Or "y") Then

Target.Offset(0, 1).FormulaR1C1 = Now()
Target.Offset(0, 2).Interior.ColorIndex = 5
Target.Offset(0, 3).Interior.ColorIndex = 4

End If
End If

End Sub


[email protected]

Excel VBA OR statement
 
Mybe you should use:
If (Target.Text = "Y" Or Target.Text = "y") Then

OR:

If (ucase(Target.Text) = "Y") Then


Gordon Rainsford[_3_]

Excel VBA OR statement
 
If Target.Text = "Y" Or Target.Text = "y" Then

Gordon Rainsford
--

wrote:

Why does the OrRstatement for "y" does not work?

Private Sub Worksheet_Change(ByVal Target As Excel.Range)

If Target.Column = 3 Then

If (Target.Text = "Y" Or "y") Then

Target.Offset(0, 1).FormulaR1C1 = Now()
Target.Offset(0, 2).Interior.ColorIndex = 5
Target.Offset(0, 3).Interior.ColorIndex = 4

End If
End If

End Sub



--
Gordon Rainsford

London UK

Chip Pearson

Excel VBA OR statement
 
The syntax is wrong. Use something like

If (Target.Text = "Y") Or (Target.Text = "y") Then

Or, to simplify and get rid of the Or altogether, you could use

If StrComp(target.Text, "y", vbTextCompare) = 0 Then
' equal
Else
' not equal
End If

Additionally, if you put 'Option Compare Text' as the first line
in your code module (outside of and before any procedure or
public variable), all text comparisons will ignore case, so you
could write

If Target.Text = "y" Then



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



wrote in message
oups.com...
Why does the OrRstatement for "y" does not work?

Private Sub Worksheet_Change(ByVal Target As Excel.Range)

If Target.Column = 3 Then

If (Target.Text = "Y" Or "y") Then

Target.Offset(0, 1).FormulaR1C1 = Now()
Target.Offset(0, 2).Interior.ColorIndex = 5
Target.Offset(0, 3).Interior.ColorIndex = 4

End If
End If

End Sub





All times are GMT +1. The time now is 12:44 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com