Condividi tramite

Eseguire una macro a seconda dell'OptionButton selezionato

Anonimo
2020-05-06T10:22:35+00:00

Ho trovato il seguente codice che funziona bene:

Sub OptionButtons()

Dim x As Control

UserForm1.Show

For Each x In UserForm1.Frame1.Controls 'Loop through the option buttons

                                        'within the Frame

    If x.Value = True Then

        MsgBox x.Caption        'Display the name of the selected

    End If                     'option button

Next

End Sub

Adesso vorrei fare in modo che se seleziono l' OptionButton1 venga eseguita la Macro1, se seleziono l' OptionButton2 venga eseguita la Macro2 ecc...

Non dovrebbe essere difficile ma non riesco a scrivere un codice che dia questo risultato.

 Grazie a chi mi aiuterà e saluti a tutti.

gildum

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
2020-05-06T10:46:05+00:00

Ciao gildum,

Ho trovato il seguente codice che funziona bene:

Sub OptionButtons()

Dim x As Control

UserForm1.Show

For Each x In UserForm1.Frame1.Controls 'Loop through the option buttons

                                        'within the Frame

    If x.Value = True Then

        MsgBox x.Caption        'Display the name of the selected

    End If                     'option button

Next

End Sub

Adesso vorrei fare in modo che se seleziono l' OptionButton1 venga eseguita la Macro1, se seleziono l' OptionButton2 venga eseguita la Macro2 ecc...

Non dovrebbe essere difficile ma non riesco a scrivere un codice che dia questo risultato.

 Grazie a chi mi aiuterà e saluti a tutti.

gildum

Prova qualcosa del genere:

'=========>>

Option Explicit

'--------->>

Private Sub CommandButton1_Click()

    Dim x As Control

    Dim sStr As String

    For Each x In UserForm1.Frame1.Controls

        If x.Value = True Then

            sStr = Replace(x.Caption, "OptionButton", "Macro")

            Application.Run sStr

        End If

    Next x

End Sub

'<<=========

===

Regards,

Norman

La risposta è stata utile?

1 persona ha trovato utile questa risposta.
0 commenti Nessun commento

3 risposte aggiuntive

Ordina per: Più utili
  1. Anonimo
    2020-05-06T18:36:38+00:00

    Ciao gildum,

    Perfetto! L' istruzione che non riuscivo a trovare era Application.Run

    Adesso ho aggiunto una riga e il codice funziona a meraviglia.

    Ecco il mio codice modificato:

    Private Sub CommandButton1_Click()

    Dim x As Control

    Dim i As Integer

    Dim sStr As String

    For Each x In UserForm1.Frame1.Controls                                       

        If x.Value = True Then

            i = x.TabIndex + 1

            sStr = "Macro" & i

            Application.Run sStr            ' Questa è la riga di codice che non

                                                        ' riuscivo a trovare!

        End If

    Next

    Unload Me

    End Sub

    Grazie Norman, sei un mago.

    Ciao e buona serata

    Ti ringrazio per il cortese riscontro.

    Alla prossima.

    ===

    Regards,

    Norman

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2020-05-06T15:31:12+00:00

    Perfetto! L' istruzione che non riuscivo a trovare era Application.Run

    Adesso ho aggiunto una riga e il codice funziona a meraviglia.

    Ecco il mio codice modificato:

    Private Sub CommandButton1_Click()

    Dim x As Control

    Dim i As Integer

    Dim sStr As String

    For Each x In UserForm1.Frame1.Controls                                       

        If x.Value = True Then

            i = x.TabIndex + 1

            sStr = "Macro" & i

            Application.Run sStr            ' Questa è la riga di codice che non

                                                        ' riuscivo a trovare!

        End If

    Next

    Unload Me

    End Sub

    Grazie Norman, sei un mago.

    Ciao e buona serata

    gildum

    La risposta è stata utile?

    0 commenti Nessun commento
  3. Anonimo
    2020-05-06T11:11:17+00:00

    Ciao gildum,

    Ho trovato il seguente codice che funziona bene:

    Sub OptionButtons()

    Dim x As Control

    UserForm1.Show

    For Each x In UserForm1.Frame1.Controls 'Loop through the option buttons

                                            'within the Frame

        If x.Value = True Then

            MsgBox x.Caption        'Display the name of the selected

        End If                     'option button

    Next

    End Sub

    Adesso vorrei fare in modo che se seleziono l' OptionButton1 venga eseguita la Macro1, se seleziono l' OptionButton2 venga eseguita la Macro2 ecc...

    Non dovrebbe essere difficile ma non riesco a scrivere un codice che dia questo risultato.

    Se vuoi eseguire una macro in risposta alla selezione di un controllo OptionButton,

    Prova qualcosa del genere:

    Nel modulo di codice della Userform, incolla

    '=========>>

    Option Explicit

    Dim myOptionButtons() As Classe1

    '--------->>

    Private Sub UserForm_Initialize()

        Dim Ctrl As Object, iCtr As Long

        For Each Ctrl In Me.Frame1.Controls

            If TypeName(Ctrl) = "OptionButton" Then

                iCtr = iCtr + 1

                ReDim Preserve myOptionButtons(1 To iCtr)

                Set myOptionButtons(iCtr) = New Classe1

                Set myOptionButtons(iCtr).oOptionButton = Ctrl

            End If

        Next Ctrl

    End Sub

    '<<=========

    Crea un modulo di classe (Alt+IM) e incolla il seguente codice:

    '=========>>

    Option Explicit

    Public WithEvents oOptionButton As msforms.OptionButton

    '--------->>

    Private Sub oOptionButton_Click()

    Dim sStr As String

    sStr = Replace(oOptionButton.Caption, "OptionButton", "Macro")

                Application.Run sStr

    End Sub

    '<<=========

    ===

    Regards,

    Norman

    La risposta è stata utile?

    0 commenti Nessun commento