【发布时间】:2018-05-25 19:46:02
【问题描述】:
我正在使用Private Sub Worksheet_Change(ByVal Target As Range) 对每个单元格中Range("AV9:AV" & lastrow) 的更改做出反应,这是一个下拉列表,其定义如下:
Dim lastrow2 As Long
Dim lastcell As Long
lastrow2 = Tabelle3.Range("A" & Rows.Count).End(xlUp).Offset(8).Row
lastcell = Tabelle3.Range("AH1048576").End(xlUp).Row
For Each Cell In Tabelle3.Range(Tabelle3.Cells(9, 48), Tabelle3.Cells(lastcell, 48))
If Cell = "" Then
Dim MyList(2) As String
MyList(0) = "Relevant"
MyList(1) = "For Discussion"
MyList(2) = "Not Relevant"
With Tabelle3.Range("AV9:AV" & lastrow2).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:=Join(MyList, Application.International(xlListSeparator))
End With
End If
Next
这些行被合并到一个宏中,该宏用数据和所有必要的功能(例如下拉字段)填充Tabelle3。
Private Sub Worksheet_Change(ByVal Target As Range) 定义如下:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lastrow As Long
lastrow = Tabelle3.Range("A" & Rows.Count).End(xlUp).Offset(8).Row
On Error Resume Next
If Not Intersect(Target, Range("AV9:AV" & lastrow)) Is Nothing And Target.Value = "Relevant" Or Target.Value = "For Discussion" Then
Application.CutCopyMode = False
Cells(Target.Row, "A").Resize(, 57).Copy
Tabelle14.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
Tabelle14.Range("A" & Rows.Count).End(xlUp).PasteSpecial xlPasteFormats
Tabelle14.Range("A" & Rows.Count).End(xlUp).PasteSpecial xlPasteColumnWidths
Application.CutCopyMode = False
End If
If Not Intersect(Target, Range("AV9:AV" & lastrow)) Is Nothing And Target.Value <> "" Then
Cells(Target.Row, "A").Resize(, 2).Copy
Tabelle10.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
Application.CutCopyMode = False
End If
'//Delete all duplicate rows
Set Rng = Tabelle10.UsedRange
Rng.RemoveDuplicates Columns:=Array(1)
End Sub
如您所见,Private Sub Worksheet_Change(ByVal Target As Range) 的第一部分“应该”只执行 If in a dropdown field in Range("AV9:AV" & lastrow) the option 'Relevant' or 'For Discussion' is selected 和第二部分 If anything is selceted,因此我使用了 Target.Value <> ""。这主要工作正常,但发生了一个错误。
如果我通过已经提到的宏将数据插入到Tabelle3,似乎Private Sub Worksheet_Change(ByVal Target As Range) 会自动为row 9 in Tabelle3 执行,我可以在Tabelle14 和Tabelle10 中找到它的数据。
有人知道这里发生了什么吗?
【问题讨论】:
标签: vba excel if-statement