PDA

View Full Version : If first conditions is NOK else go to second condition



Master_Viper
07-20-2016, 02:58 PM
Hello Expert,

Please help configure out this code. If the first code condition are OK the 3Spring will display in the S448 address(cell), but if the first code are not satisfied. The second condition will
applied in the same address.

If sh1.Range("S448") = "N" Or sh1.Range("S448") = "ONT" Or sh1.Range("S448") = "PON" Then b = "3Spring"
Else: sh1.Range("S448") = "Nest3" Then b = "4Spring"

Thanks for your help.

Leith Ross
07-20-2016, 05:14 PM
Hello Master_Viper,

When testing multiple conditions for a single variable, using the Select Case statement is often a better option. Here is how your code would look using this statement. It much easier to read and to expand conditions.



Select Case sh1.Range("S448").Value
Case Is = "N", "ONT", "PON"
b = "3Spring"
Case Is = "Nest3"
b = "4Spring"
Case Else
b = ""
End Select

sh1.Range("S448").Value = b

snb
07-20-2016, 11:52 PM
Alternatively written:


Select Case sh1.Range("S448").Value
Case "N", "ONT", "PON"
b = "3Spring"
Case "Nest3"
b = "4Spring"
End Select



Alternative approach


b=3-(len(sh1.Range("S448").Value)=5)&"Spring"

Master_Viper
07-21-2016, 05:24 AM
Hello Expert,

I tried the code above and unluckily the code are not working/displaying base on my conditions. Could you please look again into my codes below.

Private Sub UPDATE_Click()
Dim sh1 As Worksheet, a, b, c As String
Set sh1 = Sheets("Sheet1")
If sh1.Range("S445") = "N" Or sh1.Range("S445") = "ONT" Or sh1.Range("S445") = "PON" Then b = "3Spring"
If sh1.Range("S445") = "Nest3" Then b = "4Spring"
a = Array(sh1.Range("E16").Value, sh1.Range("E6").Value, sh1.Range("E4").Value, Mid(sh1.Range("E4"), 10, 4), sh1.Range("E431").Value, Mid(sh1.Range("E431"), 4, 7), _
sh1.Range("E437").Value, sh1.Range("S445").Value, sh1.Range("G7").Value, _
sh1.Range("N23").Value, sh1.Range("E23").Value, Mid(sh1.Range("E23"), 5, 99), sh1.Range("S448").Value, b)
With Sheets("Sheet2").Cells(Rows.Count, 4).End(xlUp).Offset(1)
.Resize(, 10).Value = a
.Offset(, -2).Value = .Row() - 2
.Offset(, -1).Value = Format(Now(), "mmmm-dd-yyyy")
End With
End Sub


Thanks,
Master_Viper

Master_Viper
07-21-2016, 07:02 AM
Problem solved!