Sviluppare funzionalita su Microsoft Office con VBA Disabilitare pulsante "X" su Excel

Login Registrati
Stai vedendo 22 articoli - dal 1 a 22 (di 22 totali)
  • Autore
    Articoli
  • #6528 Score: 0 | Risposta

    kipudda

      Buongiorno a tutti,

      sul mio file Excel ho inserito la seguente routine (entrando su VBA in "Questa_cartella_di_lavoro) per disabilitare il pulsante "X" di chiusura del file:

      Private Sub Workbook_BeforeClose(Cancel As Boolean)
      If bln Then
      Cancel = False
      Else
      Cancel = True
      End If
      End Sub

      Successivamente ho creato un pulsante apposito per procedere alla chiusura:

      Sub Chiudi()
      
      If Workbooks.Count > 1 Then
      ThisWorkbook.Close
      Else
      Application.Quit
      End If
      
      End Sub

      La prima istruzione mi funziona (inibisce correttamente il tasto "X") ma il pulsante che ho creato appositamente con la funzione Sub Chiudi() non riesce ad avviare la chiusura.

      Come mai? in cosa sbaglio?

      Grazie mille!

       

       

       

      #6530 Score: 0 | Risposta

      patel
      Moderatore
        50 pts
        #6537 Score: 0 | Risposta

        vecchio frac
        Senior Moderator
          238 pts

          Allega anche un pezzo di file di esempio altrimenti rischio di creare uno scenario diverso, perchè nemmeno io capisco cosa sia "bln".

          #6538 Score: 0 | Risposta

          kipudda

            Grazie mille per il tuo riscontro.

            ho provato ad inserire nel pulsante la routine indicata nell'articolo, a riportare le istruzioni nel modulo generale come indicato e a modificare l'istruzione presente in "Questa_cartella_di_lavoro" nel seguente modo:

            Ok2Close = True

            If Workbooks.Count > 1 Then
            ThisWorkbook.Close
            Else
            Application.Quit

            End If

            Ok2Close = False

            End Sub

            -----------

            Public Ok2Close As Boolean

            -----------

            Private Sub Workbook_BeforeClose(Cancel As Boolean)
            Cancel = Not Ok2Close
            End Sub

            ma il risultato non cambia..... il pulsante che ho creato per la chiusura del file non funziona.

             

            #6540 Score: 0 | Risposta

            vecchio frac
            Senior Moderator
              238 pts

              Il pulsante di chiusura che hai creato è un controllo modulo o un ActiveX?

              cioè come lo hai creato?

              #6542 Score: 0 | Risposta

              vecchio frac
              Senior Moderator
                238 pts

                Che versione di Excel usi?

                #6543 Score: 0 | Risposta

                kipudda

                  il pulsante è un controllo modulo.

                  la versione di Excel è del 2010.

                  #6544 Score: 0 | Risposta

                  patel
                  Moderatore
                    50 pts

                    vecchio frac wrote:Allega anche un pezzo di file di esempio altrimenti rischio di creare uno scenario diverso, perchè nemmeno io capisco cosa sia "bln".

                    se non alleghi non risolviamo, comunque io ho seguito alla lettera le indicazioni del link e funziona

                    #6546 Score: 0 | Risposta

                    vecchio frac
                    Senior Moderator
                      238 pts

                      Io avevo sviluppato una piccola routine per disabilitare la X di chiusura in Access, che però in Excel funziona solo manualmente (cioè non si avvia coll'evento Workbook_Open e non so perchè. Avviandola manualmente funziona).

                      Se vi interessa vi posto il codice. Ma ora vado a pranzo 😀

                      #6550 Score: 0 | Risposta

                      kipudda

                        Questo è il codice inserito sul pulsante di chiusura:

                        Sub Chiudi()
                        
                        Beep
                        
                        Range("A1").Select
                        Range("C6").Select
                        
                        Application.ScreenUpdating = False
                        
                        If ActiveWindow.Zoom = 120 Then
                        Range("C6").Select
                        ActiveWindow.Zoom = 100
                        Range("C6").Select
                                
                        ActiveSheet.Next.Select
                        ActiveWindow.Zoom = 100
                        ActiveSheet.Previous.Select
                        Range("C6").Select
                        
                        End If
                              
                            If Range("C6") > 0 Then
                            Range("C6").ClearContents
                            End If
                            
                            If Range("C7") > 0 Then
                            Range("C7").ClearContents
                            End If
                            
                            If Range("C8") > 0 Then
                            Range("C8").ClearContents
                            End If
                            
                            If Range("C9") > 0 Then
                            Range("C9").ClearContents
                            End If
                            
                            If Range("C10") > 0 Then
                            Range("C10").ClearContents
                            End If
                            
                            If Range("C11") > 0 Then
                            Range("C11").ClearContents
                            End If
                            
                            If Range("F6") > 0 Then
                            Range("F6").ClearContents
                            End If
                            
                            If Range("F7") > 0 Then
                            Range("F7").ClearContents
                            End If
                            
                            If Range("F8") > 0 Then
                            Range("F8").ClearContents
                            End If
                        
                            If Range("F9") > 0 Then
                            Range("F9").ClearContents
                            End If
                         
                            If Range("F10") > 0 Then
                            Range("F10").ClearContents
                            End If
                                
                            
                            If Range("F11") > 0 Then
                            Range("F11").ClearContents
                            End If
                                
                                
                            If Range("H6") > 0 Then
                            Range("H6").ClearContents
                            End If
                         
                            If Range("H7") > 0 Then
                            Range("H7").ClearContents
                            End If
                        
                            If Range("H8") > 0 Then
                            Range("H8").ClearContents
                            End If
                        
                            If Range("H9") > 0 Then
                            Range("H9").ClearContents
                            End If
                        
                            If Range("H10") > 0 Then
                            Range("H10").ClearContents
                            End If
                        
                            If Range("H11") > 0 Then
                            Range("H11").ClearContents
                            End If
                                
                                
                            If Range("J50") > 0 Then
                            Range("K6") = "=IFERROR(IF(AND(RC[-8]>0,RC[-5]>0,RC[-5]>Riferimenti!R29C5,RC[-5]<=WORKDAY((RC[-8]+90)-1,1,Riferimenti!R171C5:R224C16)),15/100,30/100),"""")"
                            End If
                            
                            If Range("J51") > 0 Then
                            Range("K7") = "=IFERROR(IF(AND(RC[-8]>0,RC[-5]>0,RC[-5]>Riferimenti!R29C5,RC[-5]<=WORKDAY((RC[-8]+90)-1,1,Riferimenti!R171C5:R224C16)),15/100,30/100),"""")"
                            End If
                            
                            If Range("J52") > 0 Then
                            Range("K8") = "=IFERROR(IF(AND(RC[-8]>0,RC[-5]>0,RC[-5]>Riferimenti!R29C5,RC[-5]<=WORKDAY((RC[-8]+90)-1,1,Riferimenti!R171C5:R224C16)),15/100,30/100),"""")"
                            End If
                            
                            If Range("J53") > 0 Then
                            Range("K9") = "=IFERROR(IF(AND(RC[-8]>0,RC[-5]>0,RC[-5]>Riferimenti!R29C5,RC[-5]<=WORKDAY((RC[-8]+90)-1,1,Riferimenti!R171C5:R224C16)),15/100,30/100),"""")"
                            End If
                            
                            If Range("J54") > 0 Then
                            Range("K10") = "=IFERROR(IF(AND(RC[-8]>0,RC[-5]>0,RC[-5]>Riferimenti!R29C5,RC[-5]<=WORKDAY((RC[-8]+90)-1,1,Riferimenti!R171C5:R224C16)),15/100,30/100),"""")"
                            End If
                            
                            If Range("J55") > 0 Then
                            Range("K11") = "=IFERROR(IF(AND(RC[-8]>0,RC[-5]>0,RC[-5]>Riferimenti!R29C5,RC[-5]<=WORKDAY((RC[-8]+90)-1,1,Riferimenti!R171C5:R224C16)),15/100,30/100),"""")"
                            End If
                                        
                            If Range("J50") = 1 Then
                            Range("J50") = ""
                            End If
                            If Range("J51") = 1 Then
                            Range("J51") = ""
                            End If
                        
                            If Range("J52") = 1 Then
                            Range("J52") = ""
                            End If
                        
                            If Range("J53") = 1 Then
                            Range("J53") = ""
                            End If
                        
                            If Range("J54") = 1 Then
                            Range("J54") = ""
                            End If
                        
                            If Range("J55") = 1 Then
                            Range("J55") = ""
                            End If
                        
                            If Range("M6") <> Worksheets("Riferimenti").Range("J278") Then
                            Range("M6") = Worksheets("Riferimenti").Range("J278")
                            End If
                                
                            If Range("M7") <> Worksheets("Riferimenti").Range("J278") Then
                            Range("M7") = Worksheets("Riferimenti").Range("J278")
                            End If
                        
                            If Range("M8") <> Worksheets("Riferimenti").Range("J278") Then
                            Range("M8") = Worksheets("Riferimenti").Range("J278")
                            End If
                        
                            If Range("M9") <> Worksheets("Riferimenti").Range("J278") Then
                            Range("M9") = Worksheets("Riferimenti").Range("J278")
                            End If
                        
                            If Range("M10") <> Worksheets("Riferimenti").Range("J278") Then
                            Range("M10") = Worksheets("Riferimenti").Range("J278")
                            End If
                        
                            If Range("M11") <> Worksheets("Riferimenti").Range("J278") Then
                            Range("M11") = Worksheets("Riferimenti").Range("J278")
                            End If
                        
                            If Range("N3") < 2 Then
                            Range("M7") = "=N7"
                            End If
                            
                            If Range("N3") < 2 Then
                            Range("M8") = "=N8"
                            End If
                            
                            If Range("N3") < 2 Then
                            Range("M9") = "=N9"
                            End If
                            
                            If Range("N3") < 2 Then
                            Range("M10") = "=N10"
                            End If
                            
                            If Range("N3") < 2 Then
                            Range("M11") = "=N11"
                            End If
                        
                            If Range("N45") > 0 Then
                            Range("N45").ClearContents
                            End If
                                   
                            If Range("N46") > 0 Then
                            Range("N46").ClearContents
                            End If
                                   
                            If Range("N47") > 0 Then
                            Range("N47").ClearContents
                            End If
                                   
                            If Range("N48") > 0 Then
                            Range("N48").ClearContents
                            End If
                                   
                            If Range("N49") > 0 Then
                            Range("N49").ClearContents
                            End If
                                   
                            If Range("N50") > 0 Then
                            Range("N50").ClearContents
                            End If
                                                     
                            If Range("V28") > 0 Or Range("H28") > 0 Or Range("G32") = "A" Then
                            Range("H28").ClearContents
                            End If
                        
                            If Range("V28") > 0 Or Range("H29") > 0 Or Range("G32") = "A" Then
                            Range("H29").ClearContents
                            End If
                        
                            If Range("V28") > 0 Or Range("H30") > 0 Or Range("G32") = "A" Then
                            Range("H30").ClearContents
                            End If
                        
                            If Range("V28") > 0 Or Range("H31") > 0 Or Range("G32") = "A" Then
                            Range("H31").ClearContents
                            End If
                         
                            If Range("V28") > 0 Or Range("H32") > 0 Or Range("G32") = "A" Then
                            Range("H32").ClearContents
                            End If
                          
                            If Range("V28") > 0 Or Range("H33") > 0 Or Range("G32") = "A" Then
                            Range("H33").ClearContents
                            End If
                        
                            If Range("W28") > 0 Or Range("K28") > 0 Or Range("G33") = "B" Then
                            Range("K28").ClearContents
                            End If
                        
                            If Range("W28") > 0 Or Range("K29") > 0 Or Range("G33") = "B" Then
                            Range("K29").ClearContents
                            End If
                        
                            If Range("W28") > 0 Or Range("K30") > 0 Or Range("G33") = "B" Then
                            Range("K30").ClearContents
                            End If
                        
                            If Range("W28") > 0 Or Range("K31") > 0 Or Range("G33") = "B" Then
                            Range("K31").ClearContents
                            End If
                        
                            If Range("W28") > 0 Or Range("K32") > 0 Or Range("G33") = "B" Then
                            Range("K32").ClearContents
                            End If
                        
                            If Range("W28") > 0 Or Range("K33") > 0 Or Range("G33") = "B" Then
                            Range("K33").ClearContents
                            End If
                        
                            If Range("G32") = "A" Then
                            Range("G32") = ""
                            End If
                               
                            If Range("G33") = "B" Then
                            Range("G33") = ""
                            End If 
                            
                            If Range("N28") > 0 Then
                            Range("N28").ClearContents
                            End If
                            
                            If Range("N29") > 0 Then
                            Range("N29").ClearContents
                            End If
                        
                            If Range("N30") > 0 Then
                            Range("N30").ClearContents
                            End If
                        
                            If Range("N31") > 0 Then
                            Range("N31").ClearContents
                            End If
                        
                            If Range("N32") > 0 Then
                            Range("N32").ClearContents
                            End If
                        
                            If Range("N33") > 0 Then
                            Range("N33").ClearContents
                            End If
                                
                            If Range("N34") <> "12/31/2014" Then
                            Range("N34") = "12/31/2014"
                            End If
                        
                            If Range("N35") > 0 Then
                            Range("N35").ClearContents
                            End If
                            
                            If Range("N36") > 0 Then
                            Range("N36").ClearContents
                            End If
                            
                            If Range("N37") > 0 Then
                            Range("N37").ClearContents
                            End If
                            
                            If Range("N38") > 0 Then
                            Range("N38").ClearContents
                            End If
                            
                            If Range("N39") > 0 Then
                            Range("N39").ClearContents
                            End If
                            
                            If Range("N40") > 0 Then
                            Range("N40").ClearContents
                            End If
                            
                            If Range("N41") > 0 Then
                            Range("N41").ClearContents
                            End If
                        
                            If Range("Q28") <> "=Riferimenti!R[99]C[-5]" Then
                            Range("Q28") = "=Riferimenti!R[99]C[-5]"
                            End If
                        
                            If Range("Q29") <> "=Riferimenti!R[98]C[0]" Then
                            Range("Q29") = "=Riferimenti!R[98]C[0]"
                            End If
                        
                            If Range("Q30") <> "=Riferimenti!R[97]C[5]" Then
                            Range("Q30") = "=Riferimenti!R[97]C[5]"
                            End If
                        
                            If Range("Q31") <> "=Riferimenti!R[96]C[10]" Then
                            Range("Q31") = "=Riferimenti!R[96]C[10]"
                            End If
                          
                            If Range("Q32") <> "=Riferimenti!R[95]C[15]" Then
                            Range("Q32") = "=Riferimenti!R[95]C[15]"
                            End If
                        
                            If Range("Q33") <> "=Riferimenti!R[94]C[20]" Then
                            Range("Q33") = "=Riferimenti!R[94]C[20]"
                            End If
                        
                            If Range("V30") <> "Per proporzione" Then
                            Range("V30") = "Per proporzione"
                            End If
                        
                            If Range("W30") <> "Per proporzione" Then
                            Range("W30") = "Per proporzione"
                            End If
                                 
                            If Range("V28") > 0 Then
                            Range("V28").ClearContents
                            End If
                        
                            If Range("W28") > 0 Then
                            Range("W28").ClearContents
                            End If
                         
                            If Range("BD6") > 0 Then
                            Range("BD6").ClearContents
                            End If
                        
                            If Range("BD7") > 0 Then
                            Range("BD7").ClearContents
                            End If
                        
                            If Range("BD8") > 0 Then
                            Range("BD8").ClearContents
                            End If
                        
                            If Range("BD9") > 0 Then
                            Range("BD9").ClearContents
                            End If
                            
                            If Range("BD10") > 0 Then
                            Range("BD10").ClearContents
                            End If
                            
                            If Range("BD11") > 0 Then
                            Range("BD11").ClearContents
                            End If
                            Manuale_1.OptionButton1 = False
                            Manuale_1.OptionButton2 = False
                            Manuale_1.OptionButton3 = False
                            Manuale_1.OptionButton4 = False
                            Manuale_1.OptionButton5 = False
                            Manuale_1.OptionButton6 = False
                            Manuale_2.OptionButton7 = False
                            Manuale_2.OptionButton8 = False
                            Manuale_2.OptionButton9 = False
                            Manuale_2.OptionButton10 = False
                            Manuale_2.OptionButton11 = False
                            Manuale_2.OptionButton12 = False
                            Manuale_3.OptionButton13 = False
                            Manuale_3.OptionButton14 = False
                            Manuale_3.OptionButton15 = False
                            Manuale_3.OptionButton16 = False
                            Manuale_3.OptionButton17 = False
                            Manuale_3.OptionButton18 = False
                            Manuale_4.OptionButton19 = False
                            Manuale_4.OptionButton20 = False
                            Manuale_4.OptionButton21 = False
                            Manuale_4.OptionButton22 = False
                            Manuale_4.OptionButton23 = False
                            Manuale_4.OptionButton24 = False
                            Manuale_5.OptionButton25 = False
                            Manuale_5.OptionButton26 = False
                            Manuale_5.OptionButton27 = False
                            Manuale_5.OptionButton28 = False
                            Manuale_5.OptionButton29 = False
                            Manuale_5.OptionButton30 = False
                            Manuale_6.OptionButton31 = False
                            Manuale_6.OptionButton32 = False
                            Manuale_6.OptionButton33 = False
                            Manuale_6.OptionButton34 = False
                            Manuale_6.OptionButton35 = False
                            Manuale_6.OptionButton36 = False
                            Ripartizione_Cumulativi.TextBox1 = ""
                            Ripartizione_Cumulativi.TextBox2 = ""
                            Ripartizione_Cumulativi.OptionButton1 = True
                            Range("V30") = Worksheets("Riferimenti").Range("D236")
                            Range("W30") = Worksheets("Riferimenti").Range("F236")
                            Ripartizione_Cumulativi.TextBox1.SetFocus
                            
                        ActiveWorkbook.Save
                        Range("A1").Select
                        Range("C6").Select
                        Ok2Close = True
                        If Workbooks.Count > 1 Then
                        ThisWorkbook.Close
                        Else
                        Application.Quit
                        End If
                        Ok2Close = False
                        Application.ScreenUpdating = True
                        End Sub

                         

                         

                         

                         

                        #6553 Score: 0 | Risposta

                        vecchio frac
                        Senior Moderator
                          238 pts

                          Troppo lungo e mal formattato... comunque ho risolto il problema, adesso sono in riunione, incollo la soluzione più tardi.

                          #6555 Score: 0 | Risposta

                          vecchio frac
                          Senior Moderator
                            238 pts

                            In Modulo1:

                            Option Explicit
                            
                            Public OK2Close As Boolean
                            
                            Sub Chiudi()
                                OK2Close = True
                                ThisWorkbook.Close
                                OK2Close = False
                            End Sub

                            Nel codice di ThisWorkbook:

                            Option Explicit
                            
                            Private Sub Workbook_BeforeClose(Cancel As Boolean)
                                Cancel = Not OK2Close
                                Application.Quit
                            End Sub
                            

                             

                            #6559 Score: 0 | Risposta

                            kipudda

                              Grazie mille!

                              Ora non posso provare la tua routine... appena possibile lo farò

                              #6614 Score: 0 | Risposta

                              kupudds

                                Ciao Vecchio Frac, ho provato con le modifiche da te suggerite, ma il pulsante di controllo non funziona ancora..... non riesce a chiudere il file

                                #6615 Score: 0 | Risposta

                                vecchio frac
                                Senior Moderator
                                  238 pts

                                  Curioso, perchè ho ripreso in mano l'esempio e fa quello che mi aspetto: Excel si chiude solo se premo il pulsantone del foglio1.

                                  Lo allego qui, dimmi se a te funziona.

                                  Allegati:
                                  You must be logged in to view attached files.
                                  #6617 Score: 0 | Risposta

                                  kipudda
                                  Partecipante

                                    effettivamente nel tuo file la macro funziona.

                                    ti segnalo che nel mio l'avevo leggermente modificare per permettere la chiusura del file senza chiudere anche gli altri file Excel eventualmente aperti:

                                    Option Explicit
                                    
                                    Public OK2Close As Boolean
                                    
                                    Sub Chiudi()
                                    .......
                                      OK2Close = True
                                      If Workbooks.Count > 1 Then
                                        ThisWorkbook.Close
                                      Else
                                        Application.Quit
                                        OK2Close = False
                                      End If
                                    End Sub

                                    non so se questo possa incidere...

                                    edit by VF: ho formattato il codice in modo più leggibile

                                    #6618 Score: 0 | Risposta

                                    kipudda
                                    Partecipante

                                      effettivamente con la tua macro il file si chiude, con la mia "ritoccata" no....

                                       

                                      #6619 Score: 0 | Risposta

                                      vecchio frac
                                      Senior Moderator
                                        238 pts

                                        E' più semplice di quello che credi.

                                        Fai solo questa modifica nel codice di ThisWorkbook:

                                        Option Explicit
                                        
                                        Private Sub Workbook_BeforeClose(Cancel As Boolean)
                                            Cancel = Not OK2Close
                                            If Workbooks.Count = 1 Then Application.Quit
                                        End Sub
                                        

                                        In pratica tutto procede come sempre. Ma esce da Excel solo quando il conteggio dei file aperti è uno solo (quello che contiene la macro).

                                        #6620 Score: 0 | Risposta

                                        kipudda
                                        Partecipante

                                          Ora funziona alla grande!!

                                          Grazie mille!

                                          #6621 Score: 0 | Risposta

                                          vecchio frac
                                          Senior Moderator
                                            238 pts

                                            😉

                                            Se pensi che la discussione sia "risolta", scegli l'apposita dicitura nella casella di riepilogo in testa a questa discussione.

                                            Grazie

                                            #6627 Score: 0 | Risposta

                                            kipudda
                                            Partecipante

                                              Scusami ma non capito dov'è la casella da selezionare....

                                              #6631 Score: 0 | Risposta

                                              vecchio frac
                                              Senior Moderator
                                                238 pts

                                                In alto, in cima alla discussione 🙂

                                                Ma vedo che qualcuno lo ha già fatto.

                                              Login Registrati
                                              Stai vedendo 22 articoli - dal 1 a 22 (di 22 totali)
                                              Rispondi a: Disabilitare pulsante "X" su Excel
                                              Gli allegati sono permessi solo ad utenti REGISTRATI
                                              Le tue informazioni: