Reputation: 1
What it does is just check the if statements and if not in this case "Nee" then write a sentence in O.
My problem: If all the cells are already filled in, and one cell needs to be changed. The macro will change other cells outcomes to which it shouldn't do and it will create duplicates.
This is how it should be when everything is filled in and if I change a cell. Like it should be
This is how it is after I change a cell like I did in L6. After I changed the value in L6 It duplicates in cell O6 and in cell O4 he duplicates that too.
Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Range("D2:L102"), Range(Target.Address)) Is Nothing Then
Call SampleMacro
End If
End Sub
Sub SampleMacro()
' Get the last row
Dim startRow As Long, lastRow As Long
startRow = 2
lastRow = Sheet3.Cells(Sheet3.Rows.Count, 1).End(xlUp).Row
For i = startRow To lastRow
' If there's Nee/Matig in D column, then append next sentence
If Sheet3.Range("D" & i).Value = "Nee" Then
Sheet3.Range("O" & i).Value = "? Er wordt in de cookie policy niet uitgelegd wat cookies zijn."
ElseIf Sheet3.Range("D" & i).Value = "Matig" Then
Sheet3.Range("O" & i).Value = "? Er wordt in de cookie policy matig uitgelegd wat cookies zijn."
End If
' If there's Nee/Matig in E column, then append next sentence with new line (Chr(10))
If Sheet3.Range("E" & i).Value = "Nee" Then
Sheet3.Range("O" & i).Value = Sheet3.Range("O" & i).Value & Chr(10) & "? Waarom ze nuttig zijn is hier niet omschreven."
ElseIf Sheet3.Range("E" & i).Value = "Matig" Then
Sheet3.Range("O" & i).Value = Sheet3.Range("O" & i).Value & Chr(10) & "? Waarom ze nuttig zijn is hier matig omschreven."
End If
' If there's Nee/Matig in F column, then append next sentence with new line (Chr(10))
If Sheet3.Range("F" & i).Value = "Nee" Then
Sheet3.Range("O" & i).Value = Sheet3.Range("O" & i).Value & Chr(10) & "? De soorten cookies die worden gebruikt zijn niet uitgelegd."
ElseIf Sheet3.Range("F" & i).Value = "Matig" Then
Sheet3.Range("O" & i).Value = Sheet3.Range("O" & i).Value & Chr(10) & "? De soorten cookies die worden gebruikt zijn matig uitgelegd."
End If
' If there's Nee/Matig in G column, then append next sentence with new line (Chr(10))
If Sheet3.Range("G" & i).Value = "Nee" Then
Sheet3.Range("O" & i).Value = Sheet3.Range("O" & i).Value & Chr(10) & "? De doeleinden zijn nergens terug te vinden."
ElseIf Sheet3.Range("G" & i).Value = "Matig" Then
Sheet3.Range("O" & i).Value = Sheet3.Range("O" & i).Value & Chr(10) & "? De doeleinden zijn matig beschreven."
End If
'If there's Nee/Matig in H column, then append next sentence with new line (Chr(10))
If Sheet3.Range("H" & i).Value = "Nee" Then
Sheet3.Range("O" & i).Value = Sheet3.Range("O" & i).Value & Chr(10) & "? Nergens wordt er gesproken over de bewaartermijn van de cookies."
ElseIf Sheet3.Range("H" & i).Value = "Matig" Then
Sheet3.Range("O" & i).Value = Sheet3.Range("O" & i).Value & Chr(10) & "? Over de bewaartermijn van de cookies wordt matig gesproken."
End If
'If there's Nee/Matig in I column, then append next sentence with new line (Chr(10))
If Sheet3.Range("I" & i).Value = "Nee" Then
Sheet3.Range("O" & i).Value = Sheet3.Range("O" & i).Value & Chr(10) & "? Hoe we de cookie-instellingen kunnen wijzigen is nergens neergeschreven."
ElseIf Sheet3.Range("I" & i).Value = "Matig" Then
Sheet3.Range("O" & i).Value = Sheet3.Range("O" & i).Value & Chr(10) & "? Hoe we de cookie-instellingen kunnen wijzigen is matig neergeschreven."
End If
'If there's Nee/Matig in J column, then append next sentence with new line (Chr(10))
If Sheet3.Range("J" & i).Value = "Nee" Then
Sheet3.Range("O" & i).Value = Sheet3.Range("O" & i).Value & Chr(10) & "? Hoe de cookiegegevens gewist kunnen worden is nergens te vinden."
ElseIf Sheet3.Range("J" & i).Value = "Matig" Then
Sheet3.Range("O" & i).Value = Sheet3.Range("O" & i).Value & Chr(10) & "? Hoe de cookiegegevens gewist kunnen worden is matig te vinden."
End If
'If there's Nee/Matig in K column, then append next sentence with new line (Chr(10))
If Sheet3.Range("K" & i).Value = "Nee" Then
Sheet3.Range("O" & i).Value = Sheet3.Range("O" & i).Value & Chr(10) & "? Er wordt niet gesproken over derde partijen."
ElseIf Sheet3.Range("K" & i).Value = "Matig" Then
Sheet3.Range("O" & i).Value = Sheet3.Range("O" & i).Value & Chr(10) & "? Er wordt matig gesproken over derde partijen."
End If
'If there's Nee/Matig in L column, then append next sentence with new line (Chr(10))
If Sheet3.Range("L" & i).Value = "Nee" Then
Sheet3.Range("O" & i).Value = Sheet3.Range("O" & i).Value & Chr(10) & "? Tenslotte is de verwerkingsverantwoordelijke niet aanwezig op het cookiebeleid."
End If
Next
End Sub
Upvotes: 0
Views: 79
Reputation: 1
Sheet 3 named "Cookie Policy", all the data is in this sheet
Module 1 where the code is for worksheet 3 named "Cookie Policy"
Break at "Application.EnableEvents = False" then this pops up
Upvotes: 0
Reputation: 5174
Your code should look like this:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Application.Intersect(Range("D2:L102"), Range(Target.Address)) Is Nothing Then
Call SampleMacro
End If
Application.EnableEvents = True
End Sub
You do this to avoid the SampleMacro
from triggering again the Worksheet_Change
event.
Edited:
To avoid doing again cells already filled you must Aim to the row you changed like this:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Application.Intersect(Range("D2:L102"), Range(Target.Address)) Is Nothing Then
Call SampleMacro(Target.Row)
End If
Application.EnableEvents = True
End Sub
This should be the code on the worksheet object. As you can see I'm including the Target.Row
variable to the SampleMacro
so the code only checks the row your cell have changed.
Option Explicit
Sub SampleMacro(i As Long)
' Get the last row
Dim Sheet3 As Worksheet
Dim j As Integer
Set Sheet3 = ThisWorkbook.Sheets("BBDD") 'whatever is called your sheet
With Sheet3
For j = 4 To 12 'For columns D to J
Select Case .Cells(i, j)
Case "Nee" 'If the cells says "Nee"
Select Case j 'depending on the column number
Case 4 'Column D
.Cells(i, "O") = "? Er wordt in de cookie policy niet uitgelegd wat cookies zijn."
Case 5 'Column E
Case 6 'Column F
Case 7 'Column G
Case 8 'Column H
Case 9 'Column I
Case 10 'Column J
Case 11 'Column K
Case 12 'Column L
End Select
Case "Matig" 'If the cells says "Matig"
Select Case j 'depending on the column number
Case 4 'Column D
.Cells(i, "O") = "? Er wordt in de cookie policy matig uitgelegd wat cookies zijn."
Case 5 'Column E
Case 6 'Column F
Case 7 'Column G
Case 8 'Column H
Case 9 'Column I
Case 10 'Column J
Case 11 'Column K
Case 12 'Column L
End Select
Next j
End With
End Sub
I didn't finish the whole code, but I hope you get how to end it. Select Case
is more readable in this case, I'm looping through columns D to L using the variable j
and one select case checking what's inside the cell "Nee"
or "Matig"
and depending on which one then the other Select Case
which checks the column number. With that your code will look cleaner and easier to read.
Upvotes: 1