Consulting

Results 1 to 5 of 5

Thread: Need beginner help with IF statement with OR

  1. #1
    VBAX Regular
    Joined
    Nov 2020
    Posts
    16
    Location

    Need beginner help with IF statement with OR

    Hi. This is my code and i get syntax error when i run it. What´s wrong and how can i fix it? I want it to be true if any of the statements exist.

    If ThisDocument.Bookmarks.Exists("Kapitel_EKP_Lantbruk1") = True Or
        ThisDocument.Bookmarks.Exists("Kapitel_EKP_Lantbruk2") = True Or
        ThisDocument.Bookmarks.Exists("Rutin_Eldning_i_det_fria") = True Or
        ThisDocument.Bookmarks.Exists("Rutin_Elinstallationer") = True Or
        ThisDocument.Bookmarks.Exists("Rutin_Gödningsmedel") = True Or
        ThisDocument.Bookmarks.Exists("Rutin_Heta_Arbeten_Lantbruk") = True Or
        ThisDocument.Bookmarks.Exists("Rutin_Hästskoning") = True Or
        ThisDocument.Bookmarks.Exists("Rutin_Högtryckstvättning") = True Or
        ThisDocument.Bookmarks.Exists("Rutin_Inomgårdsutrustning") = True Or
        ThisDocument.Bookmarks.Exists("Rutin_Insatsplan") = True Or
        ThisDocument.Bookmarks.Exists("Rutin_Lagring") = True Or
        ThisDocument.Bookmarks.Exists("Rutin_Motordrivna_fordon") = True Or
        ThisDocument.Bookmarks.Exists("Rutin_Släckutrustning") = True Or
        ThisDocument.Bookmarks.Exists("Rutin_Torkfläktar") = True Or
        ThisDocument.Bookmarks.Exists("Rutin_Uppvärmning") = True Or
        ThisDocument.Bookmarks.Exists("Rutin_Utrymning") = True) Then
    UserForm1.OB_Verksamhet_Lantbruk.Visible = True
        Else
        UserForm1.OB_Verksamhet_Lantbruk.Visible = False
        End If
    Last edited by Paul_Hossler; 12-06-2020 at 05:11 AM.

  2. #2
    VBAX Regular
    Joined
    Nov 2020
    Posts
    16
    Location
    This is a followup from my original post. I got it to work by thinking backwards but the code is ugly as ****. As i said in the header: I am a beginner All the End ifs is because Vba seems to need and End if for every If.
    If ThisDocument.Bookmarks.Exists("Kapitel_EKP_Lantbruk1") = False Then
    If ThisDocument.Bookmarks.Exists("Kapitel_EKP_Lantbruk2") = False Then
    If ThisDocument.Bookmarks.Exists("Rutin_Eldning_i_det_fria") = False Then
    If ThisDocument.Bookmarks.Exists("Rutin_Elinstallationer") = False Then
    If ThisDocument.Bookmarks.Exists("Rutin_Gödningsmedel") = False Then
    If ThisDocument.Bookmarks.Exists("Rutin_Heta_Arbeten_Lantbruk") = False Then
    If ThisDocument.Bookmarks.Exists("Rutin_Hästskoning") = False Then
    If ThisDocument.Bookmarks.Exists("Rutin_Högtryckstvättning") = False Then
    If ThisDocument.Bookmarks.Exists("Rutin_Inomgårdsutrustning") = False Then
    If ThisDocument.Bookmarks.Exists("Rutin_Insatsplan") = False Then
    If ThisDocument.Bookmarks.Exists("Rutin_Lagring") = False Then
    If ThisDocument.Bookmarks.Exists("Rutin_Motordrivna_fordon") = False Then
    If ThisDocument.Bookmarks.Exists("Rutin_Släckutrustning") = False Then
    If ThisDocument.Bookmarks.Exists("Rutin_Torkfläktar") = False Then
    If ThisDocument.Bookmarks.Exists("Rutin_Uppvärmning") = False Then
    If ThisDocument.Bookmarks.Exists("Rutin_Utrymning") = False Then
        UserForm1.OB_Verksamhet_Lantbruk.Visible = False
        Else
        UserForm1.OB_Verksamhet_Lantbruk.Visible = True
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
        End If
    Last edited by Paul_Hossler; 12-06-2020 at 05:11 AM.

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    You can mark your macro with CODE tags by using the [#] icon and pasting the macro between them

    These are not the most elegant way, but should work

    Option Explicit
    
    
    Sub abc()
    
    
        UserForm1.OB_Verksamhet_Lantbruk.Visible = False
    
    
        With ThisDocument.Bookmarks
            If .Exists("Kapitel_EKP_Lantbruk1") Then GoTo OK
            If .Exists("Kapitel_EKP_Lantbruk2") Then GoTo OK
            If .Exists("Rutin_Eldning_i_det_fria") Then GoTo OK
            If .Exists("Rutin_Elinstallationer") Then GoTo OK
            If .Exists("Rutin_Gödningsmedel") Then GoTo OK
            If .Exists("Rutin_Heta_Arbeten_Lantbruk") Then GoTo OK
            If .Exists("Rutin_Hästskoning") Then GoTo OK
            If .Exists("Rutin_Högtryckstvättning") Then GoTo OK
            If .Exists("Rutin_Inomgårdsutrustning") Then GoTo OK
            If .Exists("Rutin_Insatsplan") Then GoTo OK
            If .Exists("Rutin_Lagring") Then GoTo OK
            If .Exists("Rutin_Motordrivna_fordon") Then GoTo OK
            If .Exists("Rutin_Släckutrustning") Then GoTo OK
            If .Exists("Rutin_Torkfläktar") Then GoTo OK
            If .Exists("Rutin_Uppvärmning") Then GoTo OK
            If .Exists("Rutin_Utrymning") Then GoTo OK
    OK:
            UserForm1.OB_Verksamhet_Lantbruk.Visible = True
        End With
    End Sub
    
    Sub def
        Dim b As Boolean
    
    
        b = False
        With ThisDocument.Bookmarks
            b = b Or .Exists("Kapitel_EKP_Lantbruk1")
            b = b Or .Exists("Kapitel_EKP_Lantbruk2")
            b = b Or .Exists("Rutin_Eldning_i_det_fria")
            b = b Or .Exists("Rutin_Elinstallationer")
            b = b Or .Exists("Rutin_Gödningsmedel")
            b = b Or .Exists("Rutin_Heta_Arbeten_Lantbruk")
            b = b Or .Exists("Rutin_Hästskoning")
            b = b Or .Exists("Rutin_Högtryckstvättning")
            b = b Or .Exists("Rutin_Inomgårdsutrustning")
            b = b Or .Exists("Rutin_Insatsplan")
            b = b Or .Exists("Rutin_Lagring")
            b = b Or .Exists("Rutin_Motordrivna_fordon")
            b = b Or .Exists("Rutin_Släckutrustning")
            b = b Or .Exists("Rutin_Torkfläktar")
            b = b Or .Exists("Rutin_Uppvärmning")
            b = b Or .Exists("Rutin_Utrymning")
        End With
            
        UserForm1.OB_Verksamhet_Lantbruk.Visible = b
    End Sub

    Yes, VBA does like (usually) to have an EndIf for every If
    You can put skip the End If when the whole conditional is on one line like I did

    With indenting, you can see the bracketing

    Sub def()
        If ThisDocument.Bookmarks.Exists("Kapitel_EKP_Lantbruk1") = False Then
            If ThisDocument.Bookmarks.Exists("Kapitel_EKP_Lantbruk2") = False Then
                If ThisDocument.Bookmarks.Exists("Rutin_Eldning_i_det_fria") = False Then
                    If ThisDocument.Bookmarks.Exists("Rutin_Elinstallationer") = False Then
                        If ThisDocument.Bookmarks.Exists("Rutin_Gödningsmedel") = False Then
                            If ThisDocument.Bookmarks.Exists("Rutin_Heta_Arbeten_Lantbruk") = False Then
                                If ThisDocument.Bookmarks.Exists("Rutin_Hästskoning") = False Then
                                    If ThisDocument.Bookmarks.Exists("Rutin_Högtryckstvättning") = False Then
                                        If ThisDocument.Bookmarks.Exists("Rutin_Inomgårdsutrustning") = False Then
                                            If ThisDocument.Bookmarks.Exists("Rutin_Insatsplan") = False Then
                                                If ThisDocument.Bookmarks.Exists("Rutin_Lagring") = False Then
                                                    If ThisDocument.Bookmarks.Exists("Rutin_Motordrivna_fordon") = False Then
                                                        If ThisDocument.Bookmarks.Exists("Rutin_Släckutrustning") = False Then
                                                            If ThisDocument.Bookmarks.Exists("Rutin_Torkfläktar") = False Then
                                                                If ThisDocument.Bookmarks.Exists("Rutin_Uppvärmning") = False Then
                                                                    If ThisDocument.Bookmarks.Exists("Rutin_Utrymning") = False Then
                                                                        UserForm1.OB_Verksamhet_Lantbruk.Visible = False
                                                                    Else
                                                                        UserForm1.OB_Verksamhet_Lantbruk.Visible = True
                                                                    End If
                                                                End If
                                                            End If
                                                        End If
                                                    End If
                                                End If
                                            End If
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End Sub
    I'm not sure that the way you had it will do what I think you want.

    If ALL .Exists = False then .Visible = False is finally executed

    If ANY .Exists = True, then the If/Then goes to the End If and the .Visible = True is bypassed
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Regular
    Joined
    Nov 2020
    Posts
    16
    Location
    Thx for the input, Paul. The code I used worked as intended for me; it was all or nothing for the true or false flag and using false instead of true as an indicator worked for me (the endresult would be the same).
    I wanted the "OB_Verksamhet_Lantbruk.Visible = True" to happen if any of the bookmarks existed and the Else insted takes over.
    "OB_Verksamhet_Lantbruk.Visible = False" is triggered if none of the bookmarks exist ie all if statements are triggered.
    I am not sure what you mean with putting all the statements in a single line to bypass the End Ifs? It would be great to lose the extra 15 End Ifs at the end of my code

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Look at Paul's abc example above. There he shows how If ... Then statements on one line don't need the close End If

    You might consider:

    Dim arrBookmarks() As String
    Dim lngIndex As Long
      arrBookmarks = Split("Kapitel_EKP_Lantbruk1,A,Kapitel_EKP_Lantbruk2,Rutin_Eldning_i_det_fria,Rutin_Elinstallationer," _
                           & "Rutin_Gödningsmedel,Rutin_Heta_Arbeten_Lantbruk,Rutin_Hästskoning,Rutin_Högtryckstvättning," _
                           & "Rutin_Inomgårdsutrustning,Rutin_Insatsplan,Rutin_Lagring,Rutin_Motordrivna_fordon," _
                           & "Rutin_Släckutrustning,Rutin_Torkfläktar,Rutin_Uppvärmning,Rutin_Utrymning", ",")
      For lngIndex = 0 To UBound(arrBookmarks)
        If ThisDocument.Bookmarks.Exists(arrBookmarks(lngIndex)) Then
          UserForm1.OB_Verksamhet_Lantbruk.Visible = True
          Exit For
        End If
      Next lngIndex
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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