Hi, I have created a Userform where I let the users select options to show charts based on their selection.
But I am facing some problem with the IF statement in VBA. I wonder if I used the IF statement correctly? Because it doesn't seem to be working as I expect.
I thought only if the conditions are true, then it will execute the code after its Then? And if it does not satisfy the condition, it will move on to the next ElseIf, and so on. Please refer to the codes below to see if the way of using IF statements is correct or not. The code is running smoothly without showing any error, but showing undesired output.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sub OptionSelection
Dim k As String
With Sheet1
If Me.optCELL1.Value And Me.optOverallType.Value And Me.optInvOut.Value Then '(After selecting CELL1, OverallType and InvOut, Excel correctly shows the chart for this selection)
Call Cell1_Overall_InvOut
ElseIf Me.optCELL2.Value And Me.optOverallType.Value And Me.optInvOut.Value Then '(After selecting CELL2, OverallType and InvOut, Excel correctly shows the chart for this selection)
Call Cell2_Overall_InvOut
ElseIf Me.optCELL1.Value And Me.optCELL2.Value And Me.optOverallType.Value And Me.optInvOut.Value Then
Call Cell1AndCell2_InvOut
'(By right, after selecting CELL1, CELL2, OverallType and InvOut, Excel will show the chart for both CELL1 and CELL2, but it shows the chart for CELL 1 only, without CELL2 (which is the same as the first IF statement. I have checked the Cell1AndCell2_InvOut sub and run my code there, it does show the chart for both CELL1 and CELL2, which means there is no problem with the VBA codes there, but there is a logic error occurring in my IF statement)
Else
k = "Insufficient Selections Made"
MsgBox k
End If
End With
End Sub
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
There is no problem when there are no repeating conditions. But when it is like this, the 2nd ELSEIF condition is exactly the same as the 1st IF condition,
with only 1 more extra condition. But then it only reads Cell1, Overall and InvOut conditions, and ignores Cell2 condition, and eventually only shows the results
for the first statement. Does anyone have any idea on what and where is the logic error here?
Hope to receive help as soon as possible.
Best Regards.
But I am facing some problem with the IF statement in VBA. I wonder if I used the IF statement correctly? Because it doesn't seem to be working as I expect.
I thought only if the conditions are true, then it will execute the code after its Then? And if it does not satisfy the condition, it will move on to the next ElseIf, and so on. Please refer to the codes below to see if the way of using IF statements is correct or not. The code is running smoothly without showing any error, but showing undesired output.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sub OptionSelection
Dim k As String
With Sheet1
If Me.optCELL1.Value And Me.optOverallType.Value And Me.optInvOut.Value Then '(After selecting CELL1, OverallType and InvOut, Excel correctly shows the chart for this selection)
Call Cell1_Overall_InvOut
ElseIf Me.optCELL2.Value And Me.optOverallType.Value And Me.optInvOut.Value Then '(After selecting CELL2, OverallType and InvOut, Excel correctly shows the chart for this selection)
Call Cell2_Overall_InvOut
ElseIf Me.optCELL1.Value And Me.optCELL2.Value And Me.optOverallType.Value And Me.optInvOut.Value Then
Call Cell1AndCell2_InvOut
'(By right, after selecting CELL1, CELL2, OverallType and InvOut, Excel will show the chart for both CELL1 and CELL2, but it shows the chart for CELL 1 only, without CELL2 (which is the same as the first IF statement. I have checked the Cell1AndCell2_InvOut sub and run my code there, it does show the chart for both CELL1 and CELL2, which means there is no problem with the VBA codes there, but there is a logic error occurring in my IF statement)
Else
k = "Insufficient Selections Made"
MsgBox k
End If
End With
End Sub
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
There is no problem when there are no repeating conditions. But when it is like this, the 2nd ELSEIF condition is exactly the same as the 1st IF condition,
with only 1 more extra condition. But then it only reads Cell1, Overall and InvOut conditions, and ignores Cell2 condition, and eventually only shows the results
for the first statement. Does anyone have any idea on what and where is the logic error here?
Hope to receive help as soon as possible.
Best Regards.