它的作用只是检查if语句,如果不是,则检查“ Nee”,然后在O中写一个句子。
我的问题:如果所有单元格都已填写,则需要更改一个单元格。宏会更改其他单元格不应该执行的结果,并将创建重复项。
当所有内容都已填写并且我更换单元格时,应该是这样。 Like it should be
就像我在L6中一样更改单元格之后,情况就是这样。 After I changed the value in L6 它在O6单元格中复制,在O4单元格中他也复制。
代码:
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
答案 0 :(得分:1)
您的代码应如下所示:
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
这样做是为了避免SampleMacro
再次触发Worksheet_Change
事件。
编辑:
为避免再次填充已填充的单元格,您必须按如下所示对准已更改的行:
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
这应该是工作表对象上的代码。如您所见,我在Target.Row
中加入了SampleMacro
变量,因此代码仅检查您的单元格已更改的行。
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
我还没有完成整个代码,但是希望您能结束它。 Select Case
在这种情况下更具可读性,我使用变量j
遍历D到L列,并选择一个条件来检查单元格"Nee"
或"Matig"
中的内容,然后取决于哪个Select Case
哪个检查列号。这样,您的代码将看起来更简洁,更易于阅读。
答案 1 :(得分:0)