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

macro inserire se vengono fatte modifiche

  • Messaggi
  • OFFLINE
    giova62
    Post: 33
    Registrato il: 29/04/2021
    Città: CITTADELLA
    Età: 62
    Utente Junior
    365/2007
    00 19/06/2022 21:21
    Ciao a tutti,
    Questa macro in:
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    inserisce in un file txt esterno se vongono fatte modifiche

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
      
     Dim name1 As String, name2 As String, name3 As String, name4 As String, name5 As String
     Dim sPath As String, sComm5 As String, sComm6 As String, sComm7 As String, sComm8 As String
         
         
        Dim fogli As Worksheet
      
        Dim iUserResponse   As Integer
    
        Dim risposta1   As String
        Dim risposta2   As String
        Dim risposta3   As String
        Dim sStatus         As String
       
        Dim val1 As String
        Dim val2 As String
        Dim val3 As String
        
        
        
    '---------------------------------------------------------------------------
     'ACCESSI/FINE SESSIONE
    
        
    
     'ACCESSI
        
        Dim CurFolder, DestFolder As String
        'Dim name1 As String
        Dim Urec   As String
        
        'Dim accessi  As String
        'name1 = Foglio6.Range("G2").Value
        'name1 = "accessi"
        
        name1 = "accessi a " & Foglio11.Range("A2").Value
          
          
        CurFolder = ActiveWorkbook.Path
            
       DestFolder = CurFolder & "\" & name1 & "\"
       If Dir(DestFolder, vbDirectory) = "" Then MkDir DestFolder
    
    Open DestFolder & "\accessi.log" For Append As #1
    'Open path & "\accessi_PROVA.log" For Append As #1
    
    
     If modificato = True Then
    
    Urec = Cells(Rows.Count, 1).End(xlUp).Row + 1
        Print #1, Application.UserName, Now & " CHIUSURA MODIFICATO"
        Print #1, "-------------------------------------------------"
        Close #1
               
               
     Else
     
     If modificato = False Then
    
    Urec = Cells(Rows.Count, 1).End(xlUp).Row + 1
        Print #1, Application.UserName, Now & " CHIUSURA NON MODIFICATO "
        Print #1, "-------------------------------------------------"
        Close #1
        
        
     End If
     End If
      
      
        
    
    '------------------------------------------------------------------------------------------------------
                   
    '------------------------------------------------------------------------------------------------------
    'per utente autorizzato
    
    
    '---------------------------------------------------------------------------
        
    '---------------------------------------------------------------------------
        
         Application.ScreenUpdating = False
         
       
     '------------------------------------------------------------------------------------------
    'backup
    
        name5 = Foglio11.Range("A2").Value
       
       sComm5 = "BACKUP"
       sComm6 = Foglio11.Range("A2").Value
       
       sComm7 = sComm6  'Foglio6.Range("B3").Value
       sComm8 = sComm5 & " - " & sComm6 'Foglio6.Range("B3").Value
       
    
    If MsgBox("Sign. " & Environ("UserName") & " vuoi il backup di:" & Chr(13) & Chr(13) & _
    "< " & sComm6 & " >?", vbQuestion + vbYesNo + vbDefaultButton2, "AVVISO!") = vbYes Then
      
          
       sPath = ThisWorkbook.Path & "\" & sComm8
        If Dir(sPath, vbDirectory) = "" Then MkDir sPath
        
       'sPath = sPath & "\" & sComm7
        'If Dir(sPath, vbDirectory) = "" Then MkDir sPath
        
       'sPath = sPath & "\" & sComm6
        'If Dir(sPath, vbDirectory) = "" Then MkDir sPath
        
        
      ThisWorkbook.SaveCopyAs sPath & "\" & Format(Now, "dd-mm-yyyy - hh.mm") & " - " & ActiveWorkbook.Name '<<< data/ora
    
    End If
            
       ' End If
                                         
                        
     '--------------------------------------------------------------------------
                        
     '--------------------------------------------------------------------------
     '
     '--------------------------------------------------------------------------
    
     
    
    
    
          Application.ScreenUpdating = True
               
               
       
        
     
    End Sub
    
    


    non funziona correttamente.

    Se accedo solo per visualizzare esce nel file txt
    -------------------------------------------------
    giovanni_g 19/06/2022 17:10:46 ACCESSO
    giovanni_g 19/06/2022 17:10:50 CHIUSURA MODIFICATO
    -------------------------------------------------
    e non va bene.

    Se accedo e faccio una modifica e poi alla chiusura dico non salvare esce nel file txt
    -------------------------------------------------
    giovanni_g 19/06/2022 17:14:54 ACCESSO
    giovanni_g 19/06/2022 17:14:57 CHIUSURA MODIFICATO
    -------------------------------------------------
    e non va bene.

    Un aiuto?
    Grazie
  • OFFLINE
    rollis13
    Post: 1.209
    Registrato il: 16/08/2015
    Città: CORDENONS
    Età: 67
    Utente Veteran
    Excel 2016-32bit Win11
    00 19/06/2022 23:04
    Succede che all'accesso, non appena la macro evento WorkBook_Open si avvia e registra l'accesso, si attiva automaticamente anche la macro evento Workbook_SheetChange che imposta la variabile 'modificato' a True che è poi quella che diversifica il dato nel file Accessi.log.
    Se tu avessi fatto almeno il Debug della macro WorkBook_Open te ne saresti accorto subito alla quarta pressione del tasto F8.
    Devi dotare la riga di codice che attiva l'evento Workbook_SheetChange dell'accoppiata Application.EnableEvents = False / Application.EnableEvents = True per evitare l'inutile segnalazione di modifica.

    Mentre per il secondo caso il fatto di salvare o meno non ha niente a che fare con quanto vedi nel file Accessi.log. Questo log viene aggiornato prima che nella macro evento Workbook_BeforeClose tu abbia la possibilità di decidere o meno di salvare il file principale.
    Anche qui se tu avessi fatto il Debug della macro Workbook_BeforeClose ne avresti notato l'ordine delle operazioni eseguite.
    [Modificato da rollis13 19/06/2022 23:14]

    ______________________________________________________________
    C'è chi fa le COSE a CASO e chi fa CASO alle COSE (Ignoto)
  • OFFLINE
    giova62
    Post: 33
    Registrato il: 29/04/2021
    Città: CITTADELLA
    Età: 62
    Utente Junior
    365/2007
    00 20/06/2022 08:09
    Ciao rollis,
    grazie dell'info.