SPOT THE DIFFERENCE! Working with IF...

spacebouncer

Board Regular
Joined
Feb 7, 2014
Messages
109
Hi! Can you help?! I thought I was getting the hang of things, perhaps I'm tired!

I've written a little code just to toggle a box between two states. The first code I tried didn't work, the second does. It would be a great if anyone could explain why the first routine doesn't work. Thanks


Code:
'''''''''''''''''''''''''''''''''''''''''''''''''
''''               Recommend HW routines
'''''''''''''''''''''''''''''''''''''''''''''''''

Sub recommendhw1()
'TODO will set a HW task
'Currently toggles between the two states

Dim selectedshape As Shape
Set selectedshape = Sheets("Seating Plans").Shapes(Application.Caller)

If selectedshape.TextFrame.Characters.Text = "Recommend Task" Then
    With selectedshape
        .TextFrame.Characters.Text = "Task Recommended"
        .Fill.ForeColor.RGB = RGB(119, 147, 60)
    End With
End If

If selectedshape.TextFrame.Characters.Text = "Task Recommended" Then
    With selectedshape
            .TextFrame.Characters.Text = "Recommend Task"
            .Fill.ForeColor.RGB = RGB(128, 100, 162)
    End With
End If

End Sub


Sub recommendmyhw1()
'TODO will set a HW task
'Currently toggles between the two states

Dim selectedshape As Shape
Set selectedshape = Sheets("Seating Plans").Shapes(Application.Caller)

If selectedshape.TextFrame.Characters.Text = "Recommend Task" Then
    With selectedshape
        .TextFrame.Characters.Text = "Task Recommended"
        .Fill.ForeColor.RGB = RGB(119, 147, 60)
    End With
Else
    If selectedshape.TextFrame.Characters.Text = "Task Recommended" Then
        With selectedshape
            .TextFrame.Characters.Text = "Recommend Task"
            .Fill.ForeColor.RGB = RGB(128, 100, 162)
        End With
    Else: Exit Sub
    End If
End If

End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
just before "If selectedshape"
first code uses "End If"
second code uses "Else"
 
Last edited:
Upvote 0
Hi

In the second code you have If ... Else ... Endif.

This means that the code will never execute both branches of the If.

Either it executes the first branch or the second.


In the first code you have 2 If's in sequence. This means that the code can execute both, if both conditions match.

This is the case when the Shape text is

"Recommend Task"

In this case the first conditions is met, because the text is "Recommend Task". The code will change the text to "Task Recommended".
Then it will execute the second If, because the text is "Task Recommended". The code will change the text again back to "Recommend Task".


In conclusion, if the shape text at the beginning is "Recommend Task"
- in the first code both If's code are executed
- in the second code only one of the outer If's is executed
</pre>
 
Upvote 0
Good spot Taul!

Thanks pgc, I couldn't see that for the life of me last night! I thought for a moment everything I thought I knew about coding was in doubt!
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top