Ciao Sergio,
Il mio problema: vorrei replicare il codice qui sotto esposto anche per le scale B, C, D, per poter visualizzare un MsgBox
per ogni scala, ma il VB se incollo lo stesso codice mi dice nome non univoco e in effetti è così, cosa mi consigliate di fare
per ovviare a questo mio errore? . Un grazie anticipato, e anche un quasi buonanotte
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
If Target.Rows.Count <> 1 Then Exit Sub
Set rng = Me.Range("C10:C92")
If Not Intersect(Target, rng) Is Nothing Then
If Target.Text = "Spese Scala A" Then
MsgBox "Prestare Attenzione"
End If
End If
Set rng = Nothing
End Sub
Il motivo per l'errore riscontrato da te è dovuto al fatto che il modulo di codice del foglio può contenere solo una istanza di una data procedura di evento.
Per superare questo problema, adattando minimamente il tuo codice, potresti provara qualcosa del genere:
'=========>>
Option Explicit
Option Compare Text
'--------->>
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng As Range
Const sTesto As String = "Spese Scala "
If Target.Rows.Count <> 1 Then Exit Sub
Set Rng = Intersect(Me.Range("C10:C92"), Target)
If Not Rng Is Nothing Then
With Rng
If .Value = "Spese Scala A" _
Or .Value = "Spese Scala B" _
Or .Value = "Spese Scala C" _
Or .Value = "Spese Scala D" Then
MsgBox "Prestare Attenzione"
End If
End With
End If
End Sub
'<<=========
Tuttavia, io preferirei la seguente versione che penso sia più flesibile e più robusta:
'=========>>
Option Explicit
'--------->>
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng As Range, rngProblema As Range, rCell As Range
Dim arrTesto() As Variant, arrSuffiso As Variant
Dim Res As Variant
Dim i As Long, UB As Long
Const sTesto As String = "Spese Scala "
Const sSuffiso As String = "A,B,C,D"
arrSuffiso = Split(sSuffiso, ",")
UB = UBound(arrSuffiso)
ReDim arrTesto(1 To UB + 1)
For i = 1 To UB + 1
arrTesto(i) = sTesto & arrSuffiso(i - 1)
Next i
Set Rng = Intersect(Me.Range("C10:C92"), Target)
If Not Rng Is Nothing Then
For Each rCell In Rng.Cells
With rCell
Res = Application.Match(.Value, arrTesto, 0)
If Not IsError(Res) Then
If rngProblema Is Nothing Then
Set rngProblema = rCell
Else
Set rngProblema = Union(rCell, rngProblema)
End If
End If
End With
Next rCell
If Not Rng Is Nothing Then
Application.CutCopyMode = False
With rngProblema
.Select
Call MsgBox( _
Prompt:="Prestare Attenzione:" _
& vbNewLine _
& .Address(False, False), _
Buttons:=vbInformation, _
Title:="REPORT")
End With
End If
End If
End Sub
'<<=========
Comunque, va notato che, il codice pubblicato da te potrebbe esssere problematico. Per dimostrare questo, prova, ad esempio, a copiare due celle da una singola riga e seleziona una cella (ad esempio, cella C10) comes destinazione. In questo caso, se le
due celle che hai copiato avessero diverse valori, non si riscontrerebbe mai un messaggio di avviso! per ovviare tal problema, vedrai che io ho dichiarito l'intervallo di interesse così:
Set Rng = Intersect(Me.Range("C10:C92"), Target)
ed ho sostituito la tua istruzione di confronto
If Target.Text = "Spese Scala A"
con:
With Rng
If .Value = "Spese Scala A"
===
Regards,
Norman
