Consulting

Results 1 to 6 of 6

Thread: Solved: Shorten code

  1. #1

    Solved: Shorten code

    is there a better or shorter/efficient way of writing this code?
    [VBA]
    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[/VBA]

  2. #2
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    Where do msg1, msg2, etc reside? Are the part of a user form, are they controls on a worksheet?

  3. #3
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    If they are on a user form you can use something like:

    [vba]
    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[/vba]

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    Dunno about shorter, but better IMO

    [vba]

    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
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    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.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    Personally, I would not use a loop for 5 items, but you have an answer, so that is good.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •