Office, Excel, VB, Nome non univoco

Anonimo
2018-05-02T19:04:15+00:00

Buonasera a Voi tutti,

                                  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

Microsoft 365 e Office | Excel | Per la casa | Windows

Domanda bloccata. Questa domanda è stata eseguita dalla community del supporto tecnico Microsoft. È possibile votare se è utile, ma non è possibile aggiungere commenti o risposte o seguire la domanda.

0 commenti Nessun commento

12 risposte

Ordina per: Più utili
  1. Anonimo
    2018-05-03T08:58:15+00:00

    Un Ciao a tutti Voi, ma Voi dormite alla notte? visto la rapidità delle vostre risposte, grazie.

    Ho letto le vostre risposte e penso do aver fatto un errore nell'esposizione del mio problema; la mia intenzione è di visualizzare MsgBox differenti per ogni scala, o un altro dato ( per esempio Addebiti Individuali ) in modo di dare un aiuto per esempio come una nota ma in automatico. si può fare?

    Adesso però provo le vostre soluzioni e poi mi rifaccio vivo. Saluti

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2018-05-02T20:55:33+00:00

    Ciao Casanmaner,

    Ops.... Norman mentre postavo la mia risposta non ho visto che avevi già postato anche tu.

    Nessun problema!

    Se ho capito quello che vuoi prova così:

    [,,,]

          With Target

            If .Value = "Spese Scala A" Or _

               .Value = "Spese Scala B" Or _

               .Value = "Spese Scala C" Or _

               .Value = "Spese Scala D" Then

    [,,,]

    Vedi i miei commenti al riguardo di eventuali problemi con l'uso di Target in questo contesto nella mia risposta a Sergio.

    ===

    Regards,

    Norman

    La risposta è stata utile?

    0 commenti Nessun commento
  3. Anonimo
    2018-05-02T20:45:58+00:00

    Ops.... Norman mentre postavo la mia risposta non ho visto che avevi già postato anche tu.

    La risposta è stata utile?

    0 commenti Nessun commento
  4. Anonimo
    2018-05-02T20:38:19+00:00

    Se ho capito quello che vuoi prova così:

    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

          With Target

            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

        Set rng = Nothing

    End Sub

    La risposta è stata utile?

    0 commenti Nessun commento
  5. Anonimo
    2018-05-02T20:31:50+00:00

    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

    La risposta è stata utile?

    0 commenti Nessun commento