Stellar Blade Un'esclusiva PS5 che sta facendo discutere per l'eccessiva bellezza della protagonista. Vieni a parlarne su Award & Oscar!

Excel Forum Per condividere esperienze su Microsoft Excel

ciclo for vba

  • Messaggi
  • OFFLINE
    Massimo CP
    Post: 5
    Registrato il: 07/02/2016
    Utente Junior
    2013
    00 07/12/2020 21:46
    Ciao
    per cortesia dove sbaglio in quanto mi restituisce errore

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim ur As Long, cella As Range
    Dim messaggio As String
    ur = Cells(Rows.Count, "n").End(xlUp).Row
    If Not Intersect(Target, Range("h2:h1900")) Is Nothing Then
    If Target.Value = "1" Or "2" Or "3" Or "4" Or "5" Or "6" Then ' numero pneus da depositare
    For Each cella In Worksheets("Deposito GOMME").Range("n2:n" & ur)
    If cella.Value = "Resina" Then
    ' Dim messaggio As String
    messaggio = MsgBox("DEVI DEPOSITARE RESINA ? ", vbYesNo + vbCritical)
    If messaggio = vbYes Then
    Cells(cella.Row, "j").Value = "DEPOSITARE"
    End If
    Next cella
    'Exit For
    End If
    End If
    End Sub
  • OFFLINE
    rollis13
    Post: 1.040
    Registrato il: 16/08/2015
    Città: CORDENONS
    Età: 67
    Utente Veteran
    Excel 2016-32bit Win11
    00 07/12/2020 22:05
    Ma il Debug l'hai fatto ? avresti visto subito dove si arenava la macro; ci sono dei cicli e/o confronti privi della chiusura.
    Io ho fatto la quadratura ma non mi sembra che la macro faccia esattamente quello che hai in mente, sempre che io abbia capito cosa intendi fare, vista la pochezza delle informazioni disponibili.
    Option Explicit
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        Dim ur     As Long, cella As Range
        Dim messaggio As String
    
        ur = Cells(Rows.Count, "N").End(xlUp).Row
        If Not Intersect(Target, Range("H2:H1900")) Is Nothing Then
            If Target.Value = "1" Or "2" Or "3" Or "4" Or "5" Or "6" Then ' numero pneus da depositare
                For Each cella In Worksheets("Deposito GOMME").Range("N2:N" & ur)
                    If cella.Value = "Resina" Then
                        messaggio = MsgBox("DEVI DEPOSITARE RESINA ? ", vbYesNo + vbCritical)
                        If messaggio = vbYes Then
                            Cells(cella.Row, "J").Value = "DEPOSITARE"
                        End If
                    End If
                Next cella
            End If
        End If
        
    End Sub
    [Modificato da rollis13 08/12/2020 00:01]

    ______________________________________________________________
    C'è chi fa le COSE a CASO e chi fa CASO alle COSE (Ignoto)
  • OFFLINE
    Massimo CP
    Post: 5
    Registrato il: 07/02/2016
    Utente Junior
    2013
    00 07/12/2020 23:40
    Re:
    rollis13, 07/12/2020 22:05:

    Ma il Debug l'hai fatto ? avresti visto subito dove si arenava la macro; ci sono dei cicli e/o confronti privi della chiusura.
    Io ho fatto la quadratura non non mi sembra che la macro faccia esattamente quello che hai in mente, sempre che io abbia capito cosa intendi fare, vista la pochezza delle informazioni disponibili.
    Option Explicit
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        Dim ur     As Long, cella As Range
        Dim messaggio As String
    
        ur = Cells(Rows.Count, "N").End(xlUp).Row
        If Not Intersect(Target, Range("H2:H1900")) Is Nothing Then
            If Target.Value = "1" Or "2" Or "3" Or "4" Or "5" Or "6" Then ' numero pneus da depositare
                For Each cella In Worksheets("Deposito GOMME").Range("N2:N" & ur)
                    If cella.Value = "Resina" Then
                        messaggio = MsgBox("DEVI DEPOSITARE RESINA ? ", vbYesNo + vbCritical)
                        If messaggio = vbYes Then
                            Cells(cella.Row, "J").Value = "DEPOSITARE"
                        End If
                    End If
                Next cella
            End If
        End If
        
    End Sub



    Grazie per la risposta
    io vorrei che, SE la colonna H contiene un valore compreso da 1,2,3,4,5 o 6 del foglio deposito gomme e che la colonna N ha un valore della stessa riga con la voce RESINA mi appaia un msgbox che mi chieda conferma di depositare gomme presso resina, se rispondo si deve inserire in automatico la voce depositare nella stessa riga della colonna J
  • OFFLINE
    tanimon
    Post: 1.370
    Registrato il: 27/06/2011
    Utente Veteran
    excel 2007
    00 08/12/2020 01:34
    ciao,

    guarda se questa va bene

    Frank

    vb
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim messaggio As String
    
    Application.ScreenUpdating = False
    
    If Not Intersect(Target, Range("H2:H1900")) Is Nothing Then
        Select Case Target.Value
           Case 1 To 6
                If Target.Offset(0, 6).Value = "Resina" Then
                    messaggio = MsgBox("DEVI DEPOSITARE RESINA ? ", vbYesNo + vbCritical)
                        If messaggio = vbYes Then
                            Target.Offset(0, 2).Value = "DEPOSITARE"
                        End If
                End If
           End Select
    End If
    Application.ScreenUpdating = True
    End Sub
    
    









    Stretta la foglia, larga la via, dite la vostra che ho detto la mia.
    Excel 2007 forse anche 2013 ... 2021 ... 365 e future...
  • OFFLINE
    Massimo CP
    Post: 6
    Registrato il: 07/02/2016
    Utente Junior
    2013
    00 08/12/2020 12:33
    Re:
    tanimon, 08/12/2020 01:34:

    ciao,

    guarda se questa va bene

    Frank

    vb
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim messaggio As String
    
    Application.ScreenUpdating = False
    
    If Not Intersect(Target, Range("H2:H1900")) Is Nothing Then
        Select Case Target.Value
           Case 1 To 6
                If Target.Offset(0, 6).Value = "Resina" Then
                    messaggio = MsgBox("DEVI DEPOSITARE RESINA ? ", vbYesNo + vbCritical)
                        If messaggio = vbYes Then
                            Target.Offset(0, 2).Value = "DEPOSITARE"
                        End If
                End If
           End Select
    End If
    Application.ScreenUpdating = True
    End Sub
    
    





    Ciao Frank
    grazie mille funziona come deve!!
    Grazie a tutti per la cortesia