1
我正在嘗試將第二個代碼添加到單個工作表,並不斷收到「檢測到不明確的名稱」錯誤。意識到我需要結合這兩個代碼,但遇到麻煩。這裏有兩個代碼,一個在另一個之下:檢測到不明確的名稱:Worksheet_change
Private Sub Worksheet_Change(ByVal Target As Range)
'are changes made within answer range?
Set isect = Application.Intersect(Target, Range("Answers"))
If Not (isect Is Nothing) Then
For Each chng In Target.Cells
'Get row number
startY = Impact.Range("Answers").Row
targetY = chng.Row
row_offset = (targetY - startY) + 1
rating_type = Impact.Range("Impacts").Cells(row_offset, 1)
If rating_type = "Major/V.High" Then cols = 16711884
If rating_type = "Significant/High" Then cols = 255
If rating_type = "Important/Moderate" Then cols = 49407
If rating_type = "Minor/Low" Then cols = 5287936
If rating_type = "" Then cols = 16777215
Impact.Range("Ratings").Cells(row_offset, 1).Interior.Color = cols
Impact.Range("Impacts").Cells(row_offset, 1).Interior.Color = cols
Next chng
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
' To Select Multiple Items from a Drop Down List in Excel
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$C$2" Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & ", " & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
希望有人知道如何在兩個爲了繞過這個錯誤結合。
在此先感謝!
在工作表模塊上不能有兩個單獨的更改事件代碼。如果必須跟蹤多個範圍內的更改,則可以在IF和ElseIf塊中的一個更改事件代碼中加入條件。由於錯誤的不明確名稱是不言自明的,即您有兩個具有相同名稱的宏。 – sktneer
您有兩個名稱相同的程序。在這一點上,這個名字是爲事件處理程序保留的。在一個代碼模塊中不能有兩個具有相同名稱的過程。如果您現在需要在工作表更改上做更多事情,請將新代碼複製到現有的「Worksheet_Change」處理程序中。只有你知道它應該如何結合已經存在的代碼。 – GSerg