È soltanto un Pokémon con le armi o è un qualcosa di più? Vieni a parlarne su Award & Oscar!
 
Pagina precedente | 1 | Pagina successiva
Vota | Stampa | Notifica email    
Autore

file modificato

Ultimo Aggiornamento: 26/08/2019 16:46
Post: 2
Registrato il: 18/08/2019
Età: 19
Utente Junior
un saluto
OFFLINE
21/08/2019 19:17

Ciao,
è possibile alla chiusura:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

if file è modificato then


msg modificato


if file non modificato then


msg non modificato


end if



fc
Post: 855
Registrato il: 16/08/2015
Città: CORDENONS
Età: 67
Utente Senior
Excel 2016-32bit Win11
ONLINE
21/08/2019 19:45

Potresti sfruttare l'evento Wookbook_SheetChange per rilevare modifiche fatte nei vari fogli. Prova con questa macro da mettere nel modulo vbe "Questa Cartella di lavoro" (ThisworkBook) come base di partenza e vedi se soddisfa la tua richiesta.
Option Explicit
Dim modificato As Boolean

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    modificato = True

End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
 
    If modificato = True Then
        MsgBox "file modificato"
    Else
        MsgBox "file non modificato"
    End If

End Sub

______________________________________________________________
C'è chi fa le COSE a CASO e chi fa CASO alle COSE (Ignoto)
Post: 2
Registrato il: 18/08/2019
Età: 19
Utente Junior
un saluto
OFFLINE
21/08/2019 20:46

Ciao rollis,
grazie della macro.
Ora la devo adattare ad un'altra macro.
Ti ringrazio tanto.
Un saluto.
fc
Post: 856
Registrato il: 16/08/2015
Città: CORDENONS
Età: 67
Utente Senior
Excel 2016-32bit Win11
ONLINE
21/08/2019 22:07

Grazie del riscontro positivo 👍. Buon proseguimento con il tuo progetto.

______________________________________________________________
C'è chi fa le COSE a CASO e chi fa CASO alle COSE (Ignoto)
Post: 3
Registrato il: 18/08/2019
Età: 19
Utente Junior
un saluto
OFFLINE
22/08/2019 12:54

Ciao rollis,
non riesco ad adattare la tua macro a questa che allego.
La macro salva in un foglio txt chi accede al workbook.
Non riesco a fare questo la terza scelta:
se apro e non modifico nulla si visualizza non modificato nel file txt e va bene
se apro e modifico qualcosa salvo si, si visualizza modificato nel file txt e va bene
se apro e modifico qualcosa salvo no, si visualizza modificato nel file txt e qui non va bene, deve visualizzarsi non modficato.
Allego esempio.
fc
[Modificato da franc.ciccio 22/08/2019 14:48]
Post: 857
Registrato il: 16/08/2015
Città: CORDENONS
Età: 67
Utente Senior
Excel 2016-32bit Win11
ONLINE
22/08/2019 23:14

Riscrivi le macro del modulo 'ThisWorkbook' così:
Option Explicit
Dim modificato As Boolean

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    modificato = True
    
End Sub

Private Sub Workbook_Open()

    Dim name1 As String
    Dim CurFolder As String
    Dim DestFolder As String

    Application.ScreenUpdating = False
    name1 = Foglio2.Range("Z1").Value
    CurFolder = ActiveWorkbook.path
    DestFolder = CurFolder & "\" & name1 & "\"
    If Dir(DestFolder, vbDirectory) = "" Then MkDir DestFolder
    Open DestFolder & "\accessi.log" For Append As #1
    Print #1, Application.UserName, Now & " ACCESSO"
    Close #1
    Application.ScreenUpdating = True
    
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Dim name1 As String
    Dim CurFolder As String
    Dim DestFolder As String
    Dim risposta As String

    name1 = Foglio2.Range("Z1").Value
    CurFolder = ActiveWorkbook.path
    DestFolder = CurFolder & "\" & name1 & "\"
    If Dir(DestFolder, vbDirectory) = "" Then MkDir DestFolder
    Open DestFolder & "\accessi.log" For Append As #1
    If modificato = True Then
        risposta = MsgBox("Vuoi salvare le modifiche apportate ?", vbYesNo)
        Select Case risposta
            Case Is = vbYes
                'file modificato e salvato
                Print #1, Application.UserName, Now & " CHIUSURA" & " modificato "
                ThisWorkbook.Save
            Case Is = vbNo
                'file modificato ma non salvato
                Print #1, Application.UserName, Now & " CHIUSURA" & " non modificato "
                ThisWorkbook.Saved = True
        End Select
    Else
        'file non modificato
        Print #1, Application.UserName, Now & " CHIUSURA" & " non modificato "
    End If
    Print #1, "-------------------------------------------------------"
    Close #1
    
End Sub

______________________________________________________________
C'è chi fa le COSE a CASO e chi fa CASO alle COSE (Ignoto)
Post: 4
Registrato il: 18/08/2019
Età: 19
Utente Junior
un saluto
OFFLINE
23/08/2019 09:11

Ciao rollis,
penso sia esatto.
E' possibile aggiungere all'avviso "vuoi salvare" il pulsanter annulla?
Ho modificato qui:

risposta = MsgBox("Salvare le modifiche apportate a '" & name2 & "'?", vbExclamation + vbYesNoCancel, "Microsoft Office Question")

ma l'annulla non funziona, nel senso che poi compare l'avviso di uscita di excel.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
 
    Dim name1, name2 As String
    Dim CurFolder As String
    Dim DestFolder As String
    Dim risposta As String
 
    name1 = Foglio2.Range("Z1").Value
    name2 = Foglio2.Range("Z3").Value
     
    CurFolder = ActiveWorkbook.path
    
    DestFolder = CurFolder & "\" & name1 & "\"
    
    If Dir(DestFolder, vbDirectory) = "" Then MkDir DestFolder
    
    Open DestFolder & "\accessi.log" For Append As #1
    
    If modificato = True Then
    
        risposta = MsgBox("Salvare le modifiche apportate a '" & name2 & "'?", vbExclamation + vbYesNoCancel, "Microsoft Office Question")
        
        Select Case risposta
        
            Case Is = vbYes
            
                'file modificato e salvato
                Print #1, Application.UserName, Now & " CHIUSURA" & " modificato "
                ThisWorkbook.Save
                
            Case Is = vbNo
                'file modificato ma non salvato
                Print #1, Application.UserName, Now & " CHIUSURA" & " non modificato "
                ThisWorkbook.Saved = True
                
        End Select
        
    Else
    
        'file non modificato
        Print #1, Application.UserName, Now & " CHIUSURA" & " non modificato "
        
    End If
    Print #1, "-------------------------------------------------------"
    Close #1
     
End Sub



da aggiungere un'altro case is

Case Is = vbCancel


qui non so come aggiugere exit sub

fc
[Modificato da franc.ciccio 23/08/2019 10:34]
Post: 858
Registrato il: 16/08/2015
Città: CORDENONS
Età: 67
Utente Senior
Excel 2016-32bit Win11
ONLINE
23/08/2019 17:20

Le variabili se poste su una sola riga vanno dichiarate singolarmente altrimenti come l'avevi dichiarate prima 'name1' assumeva un valore generico mentre 'name2' era String. Non sempre è basilare ma evita di ritrovarsi con comportamenti strani poi non sempre facilmente individuabili.
La macro la puoi riscrivere così:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Dim name1 As String, name2 As String          'modificata
    Dim CurFolder As String
    Dim DestFolder As String
    Dim risposta As String

    name1 = Foglio2.Range("Z1").Value
    name2 = Foglio2.Range("Z3").Value
    CurFolder = ActiveWorkbook.path
    DestFolder = CurFolder & "\" & name1 & "\"
    If Dir(DestFolder, vbDirectory) = "" Then MkDir DestFolder
    Open DestFolder & "\accessi.log" For Append As #1
    If modificato = True Then
        risposta = MsgBox("Salvare le modifiche apportate a '" & name2 & "' ?", vbExclamation + vbYesNoCancel, "Microsoft Office Question")
        Select Case risposta
            Case Is = vbYes
                'file modificato e salvato
                Print #1, Application.UserName, Now & " CHIUSURA" & " modificato "
                ThisWorkbook.Save
            Case Is = vbNo
                'file modificato ma non salvato
                Print #1, Application.UserName, Now & " CHIUSURA" & " non modificato "
                ThisWorkbook.Saved = True
            Case Is = vbCancel
                'uscita annullata
                Cancel = True                     'annullo l'evento Close
                Close #1                          'chiudo il file accessi.log
                Exit Sub                          'abbandono la macro
        End Select
    Else
        'file non modificato
        Print #1, Application.UserName, Now & " CHIUSURA" & " non modificato "
    End If
    Print #1, "-------------------------------------------------------"
    Close #1
    
End Sub

______________________________________________________________
C'è chi fa le COSE a CASO e chi fa CASO alle COSE (Ignoto)
Post: 5
Registrato il: 18/08/2019
Età: 19
Utente Junior
un saluto
OFFLINE
23/08/2019 19:52

Grazie rollis funziona. [SM=g27811]
Provando la nuova macro in un workbook con molte macro l'avviso finale di excel "vuoi salvare ?" compare.
Ho pensato per non visualizzarlo di mettere alla fine:

 
  ActiveWorkbook.Close False
     
     
End Sub


funziona in parte.
Non compare più l'avviso di excel ma compaiono 2 volte gli avvisi della tua macro.


Private Sub Workbook_BeforeClose(Cancel As Boolean)
 
    Dim name1 As String, name2 As String          'modificata
    Dim CurFolder As String
    Dim DestFolder As String
    Dim risposta As String
 
 
 
 
 
 
 
    name1 = Foglio2.Range("Z1").Value
    name2 = Foglio2.Range("Z3").Value
    
    CurFolder = ActiveWorkbook.path
    DestFolder = CurFolder & "\" & name1 & "\"
    
    If Dir(DestFolder, vbDirectory) = "" Then MkDir DestFolder
    
    Open DestFolder & "\accessi.log" For Append As #1
    
    If modificato = True Then
    
        risposta = MsgBox("Salvare le modifiche apportate a '" & name2 & "' ?", vbExclamation + vbYesNoCancel, "Microsoft Office Question")
        
        Select Case risposta
        
            Case Is = vbYes
                'file modificato e salvato
                Print #1, Application.UserName, Now & " CHIUSURA" & " modificato "
                ThisWorkbook.Save
                
            Case Is = vbNo
                'file modificato ma non salvato
                Print #1, Application.UserName, Now & " CHIUSURA" & " non modificato "
                ThisWorkbook.Saved = True
                
            Case Is = vbCancel
                'uscita annullata
                Cancel = True                     'annullo l'evento Close
                Close #1                          'chiudo il file accessi.log
                Exit Sub
                'abbandono la macro
                
        End Select
        
    Else
    
        'file non modificato
        Print #1, Application.UserName, Now & " CHIUSURA" & " non modificato "
    End If
    Print #1, "-------------------------------------------------------"
    Close #1
     
     
     
  
  ActiveWorkbook.Close False '<<< aggiunta
     
     
End Sub

Post: 859
Registrato il: 16/08/2015
Città: CORDENONS
Età: 67
Utente Senior
Excel 2016-32bit Win11
ONLINE
23/08/2019 22:00

E certo, con quella funzione riattivi l'evento BeforeClose e l'intero ciclo si ripete.
Ma tu non fai il debug delle tue modifiche per valutarne la funzionalità ?
Nella macro che stai testando o modificando devi mettere uno Stop o dei punti di interruzione cliccando di lato a sinistra o con il tasto F9 nelle righe più significative. Poi procedi avviando la macro e quando il VBE si ferma in Debug procedi lentamente con il tasto F8 e tieni d'occhio anche il valore delle variabili compreso tutto quello che succede riga per riga. Con F5 procedi rapidamente fino al successivo stop o fino alla fine della macro.

In questo caso specifico devi evitare il ripetersi dell'innesco dell'evento 'BeforeClose' pertanto la modifica che vuoi apportare deve essere completata con tanto di blocco e successivo sblocco, così:
    Application.EnableEvents = False              'disabiliti il controllo degli eventi
    ActiveWorkbook.Close False                    '<<< aggiunta
    Application.EnableEvents = True               'riabiliti il controllo degli eventi

______________________________________________________________
C'è chi fa le COSE a CASO e chi fa CASO alle COSE (Ignoto)
Post: 6
Registrato il: 18/08/2019
Età: 19
Utente Junior
un saluto
OFFLINE
24/08/2019 08:48

Grazie rollis è o.k. [SM=g27811]
Un saluto.
fc
Post: 860
Registrato il: 16/08/2015
Città: CORDENONS
Età: 67
Utente Senior
Excel 2016-32bit Win11
ONLINE
24/08/2019 08:54

Grazie del riscontro positivo 👍.

______________________________________________________________
C'è chi fa le COSE a CASO e chi fa CASO alle COSE (Ignoto)
Post: 7
Registrato il: 18/08/2019
Età: 19
Utente Junior
un saluto
OFFLINE
24/08/2019 12:52

Ciao ho provato ad aprire 2 file diversi di excel tra cui uno come l'allegato dove ci sono le macro di questo thread.
Chiudendo i file excel va in crash.
fc
Post: 8
Registrato il: 18/08/2019
Età: 19
Utente Junior
un saluto
OFFLINE
24/08/2019 16:16

Girando in rete ho trovato questa soluzione:


Private Sub Workbook_BeforeClose(Cancel As Boolean)

  Cancel = True
  CloseNoSave

End Sub 


Private Sub CloseNoSave

Application.EnableEvents = False
ThisWorkbook.Close SaveChanges:=False
Application.EnableEvents = True

End Sub


che funziona almeno per le trove che ho fatto, non va in crash.
Solo che se apro 2 file con la stessa macro chiudo il primo e gli avvisi della macro si visualizzano.
Chiudo il secondo gli avvisi della macro non si vusualizzano ma si visualizza l'avviso di excel normale. [SM=g27826]
fc
[Modificato da franc.ciccio 24/08/2019 16:28]
Post: 861
Registrato il: 16/08/2015
Città: CORDENONS
Età: 67
Utente Senior
Excel 2016-32bit Win11
ONLINE
24/08/2019 18:59

Il problema è il "Application.EnableEvents = False" prima della chiusura del file con "ThisWorkbook.Close SaveChanges:=False". Questa operazione di chiusura che hai introdotto di recente (nel post #9) non da il tempo alla macro di ripristinare il controllo degli eventi "Application.EnableEvents = True" (non verrà mai eseguita). Usando un solo file non ci si accorge ma se ne hai aperti più di uno anche negli altri smette di funzionare il controllo degli eventi.
[Modificato da rollis13 24/08/2019 19:01]

______________________________________________________________
C'è chi fa le COSE a CASO e chi fa CASO alle COSE (Ignoto)
Post: 9
Registrato il: 18/08/2019
Età: 19
Utente Junior
un saluto
OFFLINE
25/08/2019 12:10

Ciao,
allora ho tolto gli avvisi della macro e ho modificato così:
Option Explicit




Dim modificato As Boolean
 
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
 
    modificato = True
 
End Sub




Private Sub Workbook_Open()
Dim Urec   As String
Dim path As String
Dim name1 As String
Dim CurFolder, DestFolder As String


'---------------------------------------------------------------------------
    'username accesso/chiusura all'avvio/chiusura
   

  name1 = Foglio1.Range("A1").Value

  CurFolder = ActiveWorkbook.path
        
   DestFolder = CurFolder & "\" & name1 & "\"
   If Dir(DestFolder, vbDirectory) = "" Then MkDir DestFolder

Open DestFolder & "\accessi_ACTIONLIST_C.log" For Append As #1
'Open path & "\accessi_PROVA.log" For Append As #1


Urec = Cells(Rows.Count, 1).End(xlUp).Row + 1
    Print #1, Application.UserName, Now & " ACCESSO"
    Close #1
           
 '--------------------------------------------------------------------------


End Sub




Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim Urec   As String
Dim path As String
Dim name1 As String
Dim CurFolder, DestFolder As String


'---------------------------------------------------------------------------
    'username accesso/chiusura all'avvio/chiusura
   
   
   
  name1 = Foglio1.Range("A1").Value

  CurFolder = ActiveWorkbook.path
        
   DestFolder = CurFolder & "\" & name1 & "\"
   If Dir(DestFolder, vbDirectory) = "" Then MkDir DestFolder

Open DestFolder & "\accessi_ACTIONLIST_C.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
           
           
 End If
 
 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 Sub



i due
If modificato = True Then
If modificato = false Then
non funzionano correttamente
anche perchè la macro:


Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
 
    modificato = True
 
End Sub



funziona solo se le celle vengono modificate e modifico qualcosa e scelgo no alla fine nel file txt risulta modificato
fc
[Modificato da franc.ciccio 25/08/2019 13:06]
Post: 10
Registrato il: 18/08/2019
Età: 19
Utente Junior
un saluto
OFFLINE
25/08/2019 14:00

Ho modificato la macro ora c'è solo l'avviso di excel alla fine:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim Urec   As String
Dim path As String
Dim name1 As String
Dim CurFolder, DestFolder As String
 
 
'---------------------------------------------------------------------------
    'username accesso/chiusura all'avvio/chiusura
    
    
    
  name1 = Foglio1.Range("A1").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 ThisWorkbook.Saved = False Then
      'ThisWorkbook.Save
              
Urec = Cells(Rows.Count, 1).End(xlUp).Row + 1
    Print #1, Application.UserName, Now & " CHIUSURA MODIFICATO"
    Print #1, "-------------------------------------------------"
    Close #1
            
            
 ElseIf ThisWorkbook.Saved = True Then
 
 
     
Urec = Cells(Rows.Count, 1).End(xlUp).Row + 1
    Print #1, Application.UserName, Now & " CHIUSURA NON MODIFICATO "
    Print #1, "-------------------------------------------------"
    Close #1
     
     
     
     Else
   
   
 If ThisWorkbook.Saved = True 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
  
 '--------------------------------------------------------------------------
  
  
End Sub



no so se è possibile la terza opzione:



Else
    
    
 If ThisWorkbook.Saved = True Then
   
Urec = Cells(Rows.Count, 1).End(xlUp).Row + 1
    Print #1, Application.UserName, Now & " CHIUSURA NON MODIFICATO "
    Print #1, "-------------------------------------------------"
    Close #1
      




modifico qualcosa nel workbook poi scelgo no nell'avviso di excel
e nel file txt deve risultare non modificato.
[Modificato da franc.ciccio 26/08/2019 17:34]
Post: 11
Registrato il: 18/08/2019
Età: 19
Utente Junior
un saluto
OFFLINE
26/08/2019 09:45

Ciao,
per la terza opzione:

modifico qualcosa nel workbook poi scelgo no nell'avviso di excel e nel file txt deve risultare "NON MODIFICATO"

questa non è esatta perchè risulta sempre "MODIFICATO" nel txt:

Else
  
  
 If ThisWorkbook.Saved = True Then
 
Urec = Cells(Rows.Count, 1).End(xlUp).Row + 1
    Print #1, Application.UserName, Now & " CHIUSURA NON MODIFICATO "
    Print #1, "-------------------------------------------------"
    Close #1


fc


Post: 12
Registrato il: 18/08/2019
Età: 19
Utente Junior
un saluto
OFFLINE
26/08/2019 16:46

Ciao,
forse non è possibile la modifica alla terza opzione?
fc
Vota:
Amministra Discussione: | Chiudi | Sposta | Cancella | Modifica | Notifica email Pagina precedente | 1 | Pagina successiva
Nuova Discussione
 | 
Rispondi
Cerca nel forum
Tag discussione
Discussioni Simili   [vedi tutte]
Feed | Forum | Bacheca | Album | Utenti | Cerca | Login | Registrati | Amministra
Tutti gli orari sono GMT+01:00. Adesso sono le 00:17. Versione: Stampabile | Mobile | Regolamento | Privacy
FreeForumZone [v.6.1] - Copyright © 2000-2024 FFZ srl - www.freeforumzone.com