Creazione Userform su file excel già esistente e strutturato con diverse macro

Anonimo
2015-05-07T14:45:59+00:00

Buongiorno a tutti.

Sono praticamente inesperto sulla creazione di userform.

Chiedo gentilmente una mano per creare sul mio file excel già strutturato con diverse macro una userform ad hoc.

Se mi spiegate come fare allego volentieri sia il file excel e sia un esempio della struttura della userform che mi servirebbe.

Ultima cosa: la userform dovrebbe partira all'apertura del file excel.

Grazie mille in anticipo a tutti.

Massimo

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
Risposta accettata dall'autore della domanda
Anonimo
2015-05-14T18:03:15+00:00

Ciao Massimo,

Sostituisci

 Private Sub UserForm_Initialize()

Dim rng As Excel.Range

con:

Private Sub UserForm_Initialize()

    Dim rng As Excel.Range

    Call deleteCaption(Me)

Se non riesci ancora di integrare il mio codice, carica un file di esempio che include la userform - i dati hanno poca importanza e possono essere cancellati - e posta un link al file qui.

===

Regards,

Norman

La risposta è stata utile?

0 commenti Nessun commento
Risposta accettata dall'autore della domanda
Anonimo
2015-05-12T20:43:23+00:00

Ciao Massimo,

no, il metodo Quit non prevede argomenti. Te ne puoi accorgere da solo se, quando digiti l'istruzione, arrivato a scrivere Application.Quit<spazio> non compaiono argomenti. Invece quando scrivi ThisWorkbook.Close<spazio> appare il suggerimento: Close([SaveChanges], [Filename], [RouteWorkbook]) a indicare che gli argomenti possibili di quel metodo sono tre e tutti facoltativi (perché racchiusi in parentesi quadra).

Quindi potresti scrivere:

Private Sub cmdClose_Click()

    ThisWorkbook.Close SaveChanges:=True

    Application.Quit

End Sub

In questo modo chiudi, salvando, la Cartella di lavoro nel cui Progetto VBA sta lo UserForm in cui viene eseguito quel codice e esci da Excel; così se ci sono altre Cartelle di lavoro aperte e non salvate ti viene proposto il solito messaggio: [Salva] [Non salvare] [Annulla].

Però, però... Non va mica bene fare così. È come se allo UserForm gli togliessimo la sedia mentre sta per sedersi.

Meglio fare così:

  • Aggiungi la variabile booleana a livello di modulo mblnQuitOnClose. Ovvero la scrivi subito dopo la lista delle costanti a livello di modulo:

Private Const mcstrColDat As String = "H"

Private mblnQuitOnClose   As Boolean

  • Nella routine-evento del pulsante cmdQuit scrivi:

Private Sub cmdQuit_Click()

    mblnQuitOnClose = True

    Unload Me

End Sub

  • E, per finire, la routine-evento che viene eseguita per ultima allo scaricamento dello UserForm:

Private Sub UserForm_Terminate()

    If mblnQuitOnClose Then

      With ThisWorkbook

        If Not .Saved Then .Save

        .Application.Quit

      End With

    End If

End Sub

La risposta è stata utile?

0 commenti Nessun commento

60 risposte aggiuntive

Ordina per: Più utili
  1. Anonimo
    2015-05-12T17:58:17+00:00

    Ciao Massimo,

    potresti per esempio piazzare un Pulsante di comando di nome cmdClose e Caption SALVA E CHIUDI.

    Questa potrebbe essere la routine-evento relativa:

    Private Sub cmdClose_Click()

        ThisWorkbook.Close SaveChanges:=True

    End Sub

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2015-05-12T17:37:12+00:00

    Piace anche a me così!

    Funziona tutto perfettamente.

    Ultimissima cosa e poi se mai avrò bisogno di modifiche ulteriori aprirò una nuova discussione più avanti...

    Inserire un tasto che mi chiude il file excel salvando tutte le modifiche come me lo puoi integrare?

    NB: La x rossa sulla userform voglio che rimanga e che faccia esattamente la sua funzione e cioè chiudere solo la userform...

    Grazie ancora

    Massimo

    La risposta è stata utile?

    0 commenti Nessun commento
  3. Anonimo
    2015-05-12T17:12:19+00:00

    RISOLTO DA SOLO.

    Ottimo! :-)

    Sì, si può fare. Ma a me le finestre dei messaggi disturbano non poco... Ma se vuoi, vedremo.

    Intanto però ti propongo una soluzione che mi pare più opportuna e meno invadente.

    1. Piazza un nuovo Pulsante di comando di nome cmdNew.
    2. Impostane la proprietà Enabled a False.
    3. Sostituisci il codice precedente con questo:

    ' Module: UserForm2

    '

    Option Explicit

    Private Const mcstrFoglio As String = "Foglio1"

    Private Const mcstrStart  As String = "A4"

    Private Const mcstrStop   As String = "A81"

    Private Const mcstrColST_ As String = "A"

    Private Const mcstrColOgg As String = "B"

    Private Const mcstrColRFR As String = "C"

    Private Const mcstrColOwn As String = "D"

    Private Const mcstrColApp As String = "E"

    Private Const mcstrColAmb As String = "G"

    Private Const mcstrColDat As String = "H"

    Private Sub cmdInserisci_Click()

    On Error GoTo ExtP

    Dim wbk As Excel.Workbook

    Dim wsh As Excel.Worksheet

    Dim r   As Long

    Dim ctl As MSForms.Control

        Set wbk = Excel.Application.ThisWorkbook

        Set wsh = wbk.Worksheets(mcstrFoglio)

        With wsh

          r = .Range(mcstrStop).End(xlUp).Row + 1

          If r <= .Range(mcstrStart).Row Then

             MsgBox "L'area dati è piena.", _

                    vbOKOnly Or vbExclamation, _

                    "Inserisci"

          Else

            .Range(mcstrColST_ & r).Value = Me.txtST.Value

            .Range(mcstrColOgg & r).Value = Me.txtoggetto.Value

            .Range(mcstrColRFR & r).Value = Me.txtRFR.Value

            .Range(mcstrColOwn & r).Value = Me.cboowner.Value

            .Range(mcstrColApp & r).Value = Me.cboapplicativo.Value

            .Range(mcstrColAmb & r).Value = Me.cboambiente.Value

            .Range(mcstrColDat & r).Value = Me.cbodata.Value

          End If

        End With

        With Me

          For Each ctl In .Controls

            With ctl

              If TypeName(ctl) = "CommandButton" Then

                If ctl Is Me.cmdNew Then

                  .Enabled = True

                  .SetFocus

                End If

              Else

                .Enabled = False

              End If

            End With

          Next

          .cmdInserisci.Enabled = False

        End With

    ExtP:

        With Err

          If .Number Then MsgBox .Description, _

                                 vbOKOnly Or vbCritical, _

                                 "ERRORE#" & .Number

        End With

        On Error Resume Next

        Set ctl = Nothing

        Set wsh = Nothing

        Set wbk = Nothing

    End Sub

    Private Sub cmdNew_Click()

    Dim ctl As MSForms.Control

        With Me

          For Each ctl In .Controls

            With ctl

              If TypeName(ctl) = "CommandButton" Then

                If ctl Is Me.cmdInserisci Then .Enabled = True

              Else

                Select Case TypeName(ctl)

                Case "ComboBox", "TextBox"

                  .Enabled = True

                  .Value = Null

                Case "Label"

                  .Enabled = True

                Case Else

                  ' DO NOTHING

                End Select

              End If

            End With

          Next

          .cmdNew.Enabled = False

          .txtST.SetFocus

        End With

        Set ctl = Nothing

    End Sub

    La risposta è stata utile?

    0 commenti Nessun commento