PDA

View Full Version : Solved: Shorten code



av8tordude
06-19-2011, 02:54 PM
is there a better or shorter/efficient way of writing this code?

If msg1.Caption = "Airport Shuttle Tips*" Or msg2.Caption = "Airport Shuttle Tips*" Or _
msg3.Caption = "Airport Shuttle Tips*" Or msg4.Caption = "Airport Shuttle Tips*" Or _
msg5.Caption = "Airport Shuttle Tips*" Then LTips.Visible = True

mbarron
06-19-2011, 04:35 PM
Where do msg1, msg2, etc reside? Are the part of a user form, are they controls on a worksheet?

mbarron
06-19-2011, 04:44 PM
If they are on a user form you can use something like:


Dim i As Integer
For i = 1 To 5
If Me.Controls("msg" & i).Caption Like "Airport Shuttle Tips*" Then
LTips.Visible = True
End If
Next

Bob Phillips
06-20-2011, 12:58 AM
Dunno about shorter, but better IMO



msgTest = "Airport Shuttle Tips*"
LTips.Visible = msg1.Caption = msgTest Or _
msg2.Caption = msgTest Or _
msg3.Caption = msgTest Or _
msg4.Caption = msgTest Or _
msg5.Caption = msgTest

av8tordude
06-20-2011, 09:36 AM
Thank you both for your help. XLD, you have tremendously help me in the past, which I am always grateful, but mbarron's code is what I was looking for.:friends:

Bob Phillips
06-20-2011, 10:23 AM
Personally, I would not use a loop for 5 items, but you have an answer, so that is good.