View Full Version : Need beginner help with IF statement with OR
magsol76
12-06-2020, 04:34 AM
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
magsol76
12-06-2020, 04:46 AM
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
Paul_Hossler
12-06-2020, 05:20 AM
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
magsol76
12-06-2020, 06:26 AM
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 :)
gmaxey
12-07-2020, 07:28 AM
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,Ruti n_Elinstallationer," _
& "Rutin_Gödningsmedel,Rutin_Heta_Arbeten_Lantbruk,Rutin_Hästskoning,Rutin_Hög tryckstvä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
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.