Consulting

Results 1 to 6 of 6

Thread: Shorten code

  1. #1

    Shorten code

    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
    Last edited by Aussiebear; 04-02-2025 at 06:10 PM.

  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:

    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
    Last edited by Aussiebear; 04-02-2025 at 06:11 PM.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    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
    Last edited by Aussiebear; 04-02-2025 at 06:12 PM.
    ____________________________________________
    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,446
    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
  •