• Privacywetgeving
    Het is bij Helpmij.nl niet toegestaan om persoonsgegevens in een voorbeeld te plaatsen. Alle voorbeelden die persoonsgegevens bevatten zullen zonder opgaaf van reden verwijderd worden. In de vraag zal specifiek vermeld moeten worden dat het om fictieve namen gaat.

'Als dan' verwerken in een macro

Status
Niet open voor verdere reacties.

Timabim

Gebruiker
Lid geworden
14 dec 2015
Berichten
24
Update 16-12: (wellicht handig als ik mijn bericht hierin update..)
Ik zou de macro graag nog verder willen uitbouwen naar onderstaand voorbeeld. Is dit mogelijk? Bij voorbaat dank!

Ik zal eerst kort uitleggen wat ik ermee wil bereiken:
ik heb 9 bedrijven onder elkaar staan; voor elk bedrijf heb ik 2 brieven (bv: word document 1B + 1B; voor elk scenario 1). Achter deze 9 bedrijven heb ik een formulierelement selectievakje gevoegd. Wanneer het selectievakje wordt aangevinkt (WAAR) alleen dan moet(en) de brief(ven) geopend worden voor de betreffende bedrijven. Ik heb zelf de macro geprobeerd bij te werken maar dat is mij helaas niet gelukt.

Voorbeeld:

Als cel B26 = "Bilateral refund" of "Bilateral reduction" EN cel C30 = WAAR open dan D:\bla\bla\word document 1A
Als cel B26 = "EU-Treaty" of "Domestic law" EN cel C30 = WAAR open dan D:\bla\bla\word document 1B
Als cel B26 = "Bilateral refund" of "Bilateral reduction" EN cel C31 = WAAR open dan D:\bla\bla\word document 2A
Als cel B26 = "EU-Treaty" of "Domestic law" EN cel C31 = WAAR open dan D:\bla\bla\word document 2B
Als cel B26 = "Bilateral refund" of "Bilateral reduction" EN cel C32 = WAAR open dan D:\bla\bla\word document 3A
Als cel B26 = "EU-Treaty" of "Domestic law" EN cel C32 = WAAR open dan D:\bla\bla\word document 3B
Als cel B26 = "Bilateral refund" of "Bilateral reduction" EN cel C33 = WAAR open dan D:\bla\bla\word document 4A
Als cel B26 = "EU-Treaty" of "Domestic law" EN cel C33 = WAAR open dan D:\bla\bla\word document 4B
....etc tm cel C38
Als cel B26 = geen van bovenstaande dan weergeven "No possibilities"
Als geen van de selectievakje is aangevinkt dan weergeven "Choose company"




Bericht 14-12:
Hallo, ik heb voor het openen van een MS Word document in excel, middels een button, reeds een mooie macro gevonden op dit forum. Nu heb ik meerdere MS Word documenten, elk behorend bij een bepaalde waarde in een cel. Nu vroeg ik mij af hoe ik onderstaande macro kan uitbreiden met een 'Als - dan' functie. Bijvoorbeeld:

Als cel B26 = "Bilateral refund" of "Bilateral reduction" open dan D:\bla\bla\word document 1
Als cel B26 = "EU-Treaty" of "Domestic law" open dan D:\bla\bla\word document 2
Als cel B26 = geen van bovenstaande dan weergeven "No possibilities"

Reeds gevonden Macro:

Sub Document_Openen()
Dim objWordApp As Object, objWordDoc As Object

Set objWordApp = CreateObject("Word.application")
objWordApp.Visible = True
Set objWordDoc = objWordApp.Documents.Open("D:\Mijn documenten\Zaak\Faktuur\Verkvoorw.doc")
End Sub

Bij voorbaat dank!

Gr tim
 
Laatst bewerkt:
Probeer dit eens....

Code:
Sub Document_Openen()
Dim objWordApp As Object, objWordDoc As Object
if [B26]= "Bilateral refund" then bestand = "D:\bla\bla\word document 1"
if [B26]= "Bilateral reduction" then bestand = "D:\bla\bla\word document 1"
if [B26]=  "EU-Treaty" OR [B26] = "Domestic law" then bestand = "D:\bla\bla\word document 2"

Set objWordApp = CreateObject("Word.application")
objWordApp.Visible = True
Set objWordDoc = objWordApp.Documents.Open(bestand)
End Sub
 
Laatst bewerkt:
Dank voor de snelle respons!

In VBA worden de eerste ([coed]Sub Document_Openen() ) en laatste regel (End Sub[/code]) rood weergegevn en bij het uitvoeren van de macro krijg ik een foutmelding:

Compileerfout:
Verwacht: expressie

In VBA wordt wordt het woord 'sub' in de eerste regel [coed]Sub Document_Openen() door VBA blauw gearceerd.

Ligt het aan mij?
 
Oeps, foutje van mij.
Kijk nu nog maar eens naar mijn antwoord.
 
@Haije
De derde voorwaarde is er onderweg afgevallen.:)
Code:
Sub Document_Openen()
On Error GoTo Oeps
Dim objWordApp As Object, objWordDoc As Object
 If [B26] = "Bilateral refund" Or [B26] = "Bilateral reduction" Then bestand = "D:\bla\bla\word document 1"
  If [B26] = "EU-Treaty" Or [B26] = "Domestic law" Then bestand = "D:\bla\bla\word document 2"
   If [B26] <> "Bilateral refund" And [B26] <> "Bilateral reduction" And [B26] <> "EU-Treaty" And [B26] <> "Domestic law" Then Msg "No Possibilities !": Exit Sub
 Set objWordApp = CreateObject("Word.application")
  objWordApp.Visible = True
 Set objWordDoc = objWordApp.Documents.Open(bestand)
Oeps:
End Sub
 
Laatst bewerkt:
Inderdaad.. was allang blij dat het eerste deel werkte.. nogmaals dank! :-)
 
Bij de laatste code toch een foutmelding:

Compileerfout:
Sub of function is niet gedefineerd

VBA arceert 'Msg' blauw in de volgende regel:

If [B26] <> "Bilateral Refund" And [B26] <> "Bilateral Reduction" And [B26] <> "EU-Treaty" And [B26] <> "Domestic law" Then Msg "No Possibilities !"
 
En VBA arceert de eerste regel geel:

Sub Document_Openen()

Die zag ik nog even over het hoofd. Bij voorbaat dank.
 
De Box is er ingebleven sorry:

Wijzig Msg in MsgBox
 
Hi, ik zou de macro graag nog verder willen uitbouwen naar onderstaand voorbeeld. Is dit mogelijk? Bij voorbaat dank!

Ik zal eerst kort uitleggen wat ik ermee wil bereiken:

ik heb 9 bedrijven onder elkaar staan; voor elk bedrijf heb ik 2 brieven (bv: word document 1B + 1B; voor elk scenario 1). Achter deze 9 bedrijven heb ik een formulierelement selectievakje gevoegd. Wanneer het selectievakje wordt aangevinkt (WAAR) alleen dan moet(en) de brief(ven) geopend worden voor de betreffende bedrijven. Ik heb zelf de macro geprobeerd bij te werken maar dat is mij helaas niet gelukt.

Voorbeeld:

Als cel B26 = "Bilateral refund" of "Bilateral reduction" EN cel C30 = WAAR open dan D:\bla\bla\word document 1A
Als cel B26 = "EU-Treaty" of "Domestic law" EN cel C30 = WAAR open dan D:\bla\bla\word document 1B
Als cel B26 = "Bilateral refund" of "Bilateral reduction" EN cel C31 = WAAR open dan D:\bla\bla\word document 2A
Als cel B26 = "EU-Treaty" of "Domestic law" EN cel C31 = WAAR open dan D:\bla\bla\word document 2B
Als cel B26 = "Bilateral refund" of "Bilateral reduction" EN cel C32 = WAAR open dan D:\bla\bla\word document 3A
Als cel B26 = "EU-Treaty" of "Domestic law" EN cel C32 = WAAR open dan D:\bla\bla\word document 3B
Als cel B26 = "Bilateral refund" of "Bilateral reduction" EN cel C33 = WAAR open dan D:\bla\bla\word document 4A
Als cel B26 = "EU-Treaty" of "Domestic law" EN cel C33 = WAAR open dan D:\bla\bla\word document 4B
....etc tm cel C38
Als cel B26 = geen van bovenstaande dan weergeven "No possibilities"
Als geen van de selectievakje is aangevinkt dan weergeven "Choose company"
 
Zet al die voorwaarden in een tabel op een (verborgen)werkblad en zet er daar het resultaat bij.
Dat kan je dan gebruiken in je VBA-code.
 
Hoi Cobbe, dank voor je reply. Ik begrijp alleen niet wat je bedoelt. Ik zie niet hoe ik de tabel moet maken die je vervolgens kan gebruiken in de VBA code. Hoe zou de eerste regel in de tabel eruit zien? Tevens weet ik niet goed hoe ik dit in VBA moet verwerken vervolgens. Zou je me daar een aantal hint voor kunnen geven? Bij voorbaat dank.
 
Vervolledig anders je macro zo:

Code:
Sub Document_Openen()
On Error GoTo Oeps

Dim objWordApp As Object, objWordDoc As Object
 
  If [B26] <> "Bilateral refund" And _
     [B26] <> "Bilateral reduction" And _
     [B26] <> "EU-Treaty" And _
     [B26] <> "Domestic law" And _
     [B26] <> "blabla" And _
     [B26] <> "blabla" And _
     [B26] <> "blabla" Then
         MsgBow "No Possibilities !": Exit Sub
 End If
 
 If [B26] = "Bilateral refund" Or [B26] = "Bilateral reduction" And [C30] = True Then bestand = "D:\bla\bla\word document 1"
 If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C30] = True Then bestand = "D:\bla\bla\word document 2"
 If [B26] = "Bilateral refund" Or "Bilateral reduction" And [C31] = True Then bestand = "D:\bla\bla\word document 3"
 If [B26] = "EU-Treaty" Or "Domestic law" And [C31] = True Then bestand = "D:\bla\bla\word document 4"
 'if
 'if
 'if
 'if
 'enz
  
  Set objWordApp = CreateObject("Word.application")
  objWordApp.Visible = True
 Set objWordDoc = objWordApp.Documents.Open(bestand)
Oeps:
End Sub
 
Ik heb de code aangepast zoals deze hoort te zijn. Eerst kreeg ik een foutmelding, deze kwam waarschijnlijk naar voren omdat 'MsgBow' was geschreven ipv 'MsgBox'. Daarna nogmaals geprobeerd en toen gebeurde er helemaal niets. Geen foutmelding maar excel opende ook geen documenten. Is er een oplossing? De code is als volgt:

Sub Document_Openen()
On Error GoTo Oeps

Dim objWordApp As Object, objWordDoc As Object

If [B26] <> "Bilateral Refund" And _
[B26] <> "Bilateral Reduction" And _
[B26] <> "EU-Treaty" And _
[B26] <> "Domestic law" Then
MsgBox "No Possibilities !": Exit Sub
End If

If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C30] = True Then bestand = "D:\bla\bla\word document 2"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C30] = True Then bestand = "D:\bla\bla\word document 3"
If [B26] = "Bilateral Refund" Or "Bilateral Reduction" And [C31] = True Then bestand = "D:\bla\bla\word document 2"
If [B26] = "EU-Treaty" Or "Domestic law" And [C31] = True Then bestand = "D:\bla\bla\word document 3"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C32] = True Then bestand = "D:\bla\bla\word document 2"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C32] = True Then bestand = "D:\bla\bla\word document 2"
If [B26] = "Bilateral Refund" Or "Bilateral Reduction" And [C33] = True Then bestand = "D:\bla\bla\word document 3"
If [B26] = "EU-Treaty" Or "Domestic law" And [C33] = True Then bestand = "D:\bla\bla\word document 4"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C34] = True Then bestand = "D:\bla\bla\word document 1"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C34] = True Then bestand = "D:\bla\bla\word document 2"
If [B26] = "Bilateral Refund" Or "Bilateral Reduction" And [C35] = True Then bestand = "D:\bla\bla\word document 3"
If [B26] = "EU-Treaty" Or "Domestic law" And [C35] = True Then bestand = "D:\bla\bla\word document 4"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C36] = True Then bestand = "D:\bla\bla\word document 1"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C36] = True Then bestand = "D:\bla\bla\word document 2"
If [B26] = "Bilateral Refund" Or "Bilateral Reduction" And [C37] = True Then bestand = "D:\bla\bla\word document 3"
If [B26] = "EU-Treaty" Or "Domestic law" And [C37] = True Then bestand = "D:\bla\bla\word document 4"
If [B26] = "Bilateral Refund" Or "Bilateral Reduction" And [C38] = True Then bestand = "D:\bla\bla\word document 3"
If [B26] = "EU-Treaty" Or "Domestic law" And [C38] = True Then bestand = "D:\bla\bla\word document 4"
Set objWordApp = CreateObject("Word.application")
objWordApp.Visible = True
Set objWordDoc = objWordApp.Documents.Open(bestand)
Oeps:
End Sub
 
Zet een vinkje (')voor On error en kijk eens waar het fout loopt.
Je kan de code doorlopen via F8

PS: Achter elke Or moet '[B26] =' komen anders klopt de vergelijking niet.
 
Laatst bewerkt:
Goede tip! Ik was er middels de F8 knop zelf al achter gekomen van de toevoeging achter Or. Maar helaas werkt het nog niet helemaal naar behoren aangezien niet alle brieven worden geopend als ik het keuze vinkje achter het bedrijf aanvink. Excel opent wel 1 brief en het is verschillend of dit van het eerste bedrijf uit de reeks is of van bijvoorbeeld de laatste in de reeks. Heb je een idee waar dat aan kan liggen?

De code is nu als volgt:

Sub Document_Openen()
On Error GoTo Oeps

Dim objWordApp As Object, objWordDoc As Object

If [B26] <> "Bilateral Refund" And _
[B26] <> "Bilateral Reduction" And _
[B26] <> "EU-Treaty" And _
[B26] <> "Domestic law" Then
MsgBox "No Possibilities !": Exit Sub
End If

If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C30] = True Then bestand = "D:\bla\bla\word document 2"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C30] = True Then bestand = "D:\bla\bla\word document 2"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C31] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C31] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C32] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C32] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C33] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C33] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C34] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C34] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C35] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C35] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C36] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C36] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C37] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C37] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "Bilateral Refund" Or [B26] = "Bilateral Reduction" And [C38] = True Then bestand = "D:\bla\bla\word document"
If [B26] = "EU-Treaty" Or [B26] = "Domestic law" And [C38] = True Then bestand = "D:\bla\bla\word document"
Set objWordApp = CreateObject("Word.application")
objWordApp.Visible = True
Set objWordDoc = objWordApp.Documents.Open(bestand)
Oeps:
End Sub
 
Als je via F8 de code doorloopt kun je toch per egel zien wat er gebeurt en of dat goed of fout is.
Zo kan je achterhalen wat er moet gebeuren, ik kan dat van hieruit niet zien.
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan