PDA

View Full Version : Solved: VBA code for "or" + "and" functions



afoehrin
04-03-2009, 11:40 AM
Dear all,

Iīm trying to create a code to import for a new sheet the values of a centain cell with a condition. For example, i would like to import the value of cell A1 if B1 is fill with certain values (S, T1, T2, T3, T4). I thought in use the function "OR" but donīt know how to write than.

The code i wrote is:

...
For i = 8 To 55
If (cont = 0) Then

If (Cells(i, 28) = "S" Or "T1" Or "T2" Or "T3" Or "T4") Then

'copia status
Range("C" & i).Copy
ActiveWindow.ActivateNext
Range("D" & k).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveWindow.ActivateNext
...

Any suggestion to help me?

Thanks and a good Weekend to all

Alan

Bob Phillips
04-03-2009, 11:45 AM
If Cells(i, 28).Value = "S" Or Cells(i, 28).Value = "T1" Or _
Cells(i, 28).Value = "T2" Or Cells(i, 28).Value = "T3" Or _
Cells(i, 28).Value = "T4" Then

p45cal
04-03-2009, 11:48 AM
2 suggestions:
1. change
If (Cells(i, 28) = "S" Or "T1" Or "T2" Or "T3" Or "T4") Then to If (Cells(i, 28) = "S" Or Cells(i, 28) = "T1" Or Cells(i, 28) = "T2" Or Cells(i, 28) = "T3" Or Cells(i, 28) = "T4") Then
2. Use
Select Case Cells(i, 28)
Case "S", "T1", "T2", "T3", "T4"
'code which does stuff
End Select

peacemaker
04-03-2009, 11:49 AM
VBA wants you to ask individual questions instead of "if this = that or that or that"
Try the line below in your code
If Cells(i, 28) = "S" Or Cells(i, 28) = "T1" Or Cells(i, 28) = "T2" Or Cells(i, 28) = "T3" Or Cells(i, 28) = "T4" Then

afoehrin
04-06-2009, 05:18 AM
Dear All,

Many thanks for all the help and support. With your help I managed to solve this issue.

I wish a nice day to everyone.

Best Regards,
Alan