Condividi tramite

Compilare celle con userform

Anonimo
2023-10-23T10:33:56+00:00

Ciao a tutti,

Ho bisogno del vostro aiuto, per compilare mediante una combo box inserita in una Userfom le celle che trovate nell'esempio allegato. Devo poter usare l'elenco, iniziando da qualsiasi nominativo. Se il nominativo ad esempio, dovesse essere l'ultimo deve ripartire dal primo. Il file lo trovate qui.

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
2023-10-29T09:45:01+00:00

Ciao Geacs.

Mi dispiace ma ho bisogno del tuo aiuto. Lavorando con il tuo codice, ho riscontrato che se l'elenco della colonna Y è più corto degli altri elenchi, influisce su tutte le altre ComboBox. In sostanza Se la ComboBox6 ha un elenco di 7 valori, mi fa vedere 7 valori a tutte le altre ComboBox. Puoi provare sul file pubblicato da te per riscontrare il problema.

Grazie per aver evidenziato un bug nel mio codice che era nascosto alla vista dal fatto che la colonna di interesse più lunga nel mio file di test era l'ultima, Inoltre, accorciando l'ultima colonna con la Userform aperta, non si è verificato il problema da te segnalato!

Fortunatamente la soluzione è molto semplice: nella procedura UserForm_Initialize, sostituisci

        With **Rng.Cells** 

            If Not IsEmpty(.Item(i, 1)) Then 

                iCtr = iCtr + 1 

                vArr(iCtr) = vArr\_Temp(i, 1) 

            End If 

        End With 

    Next i 

con:-

        With **vArr\_Elenchi(ii).Cells** 

            If Not IsEmpty(.Item(i, 1)) Then 

                iCtr = iCtr + 1 

                vArr(iCtr) = vArr\_Temp(i, 1) 

            End If 

        End With 

    Next i 

Ora, facendo clic sulla ComboBox6, si vede:

  [![Immagine](https://learn-attachment.microsoft.com/api/attachments/cc44ea1b-1172-4d3a-9178-8d5674f15624?platform=QnA"https://1drv.ms/x/s!AmTW9HzZG8cql2P7yP1AGBIFXbWn?e=FhicGT" title="1drv.ms" rel="ugc nofollow">Geac20231026.xlsm

===

Regards,

Norman

Immagine

La risposta è stata utile?

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

Risposta accettata dall'autore della domanda

Anonimo
2023-10-26T16:47:59+00:00

Ciao Geacs,

Ciao Norman, ti ringrazio ancora di quest'ultimo codice pubblicato.

Bene! Grazie a te del cortese riscontro.

Visto che sei stato tu a stuzzicare la mia fame di perfezionare quello che già funziona benissimo, se dovessi utilizzarlo per aggiornare in un'altra cartella di lavoro che contiene una userform con 6 ComboBox numerate da ComboBox1 a ComboBox6. Come modificarlo per fare in modo che si aggiornino?

Sarebbe stato utile avere un file di esempio che mostrasse la configurazione dei dati delle liste che alimentano i sei controlli ComboBox😊

Tuttavia, almeno come punto di partenza, prova quanto segue:

Nel modulo di codice Modulo1:

'========>>

Option Explicit

Public vArr_Arrays As Variant

Public vArr1() As Variant, vArr2() As Variant, vArr3() As Variant, vArr4() As Variant, vArr5() As Variant, vArr6() As Variant

Public bFlag As Boolean

Public UForm As UserForm

Public Rng_Elenchi As Range

Public vArr_Combo() As Variant

Public Const sPrime_Celle_Elenchi As String = "Q2,S2,U2,W2,X2,Y2" '<<=== Modifica

'-------->>

Public Sub Avvia_Userform()

bFlag = True 

UserForm1.Show vbModeless 

End Sub

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

Public Function LastRow(SH As Worksheet, _

Optional Rng As Range, \_ 

Optional minRow As Long = 1) 

If Rng Is Nothing Then 

    Set Rng = SH.Cells 

End If 

On Error Resume Next 

LastRow = Rng.Find(What:="\*", \_ 

    After:=Rng.Cells(1), \_ 

    Lookat:=xlPart, \_ 

    LookIn:=xlFormulas, \_ 

    SearchOrder:=xlByRows, \_ 

    SearchDirection:=xlPrevious, \_ 

    MatchCase:=False).Row 

On Error GoTo 0 

If LastRow &lt; minRow Then 

    LastRow = minRow 

End If 

End Function

'<<========

Nel modulo di codice del Foglio1

'========>>

Option Explicit

'-------->>

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Rng As Range, Rng2 As Range, rCell As Range 

Dim myUserform As UserForm 

Dim CBox As MSForms.ComboBox 

Dim vArr\_Temp As Variant 

Dim vSplit As Variant 

Dim Res As Variant 

Dim i As Long, iCtr As Long, jCtr As Long 

Dim LRow As Long, iCol As Long 

On Error Resume Next 

Set myUserform = UForm 

If myUserform Is Nothing Then Exit Sub 

On Error GoTo 0 

If Not bFlag Then Exit Sub 

Set Rng = Me.Range(sPrime\_Celle\_Elenchi) 

Set Rng2 = Intersect(Rng.EntireColumn, Target) 

If Not Rng2 Is Nothing Then 

    vSplit = Split(Rng.Address, ",") 

    Res = Application.Match(Rng2.Address, vSplit) 

    LRow = LastRow(Me, Rng2.EntireColumn) 

    With Rng 

        Set Rng\_Elenchi = Rng2.EntireColumn.Cells(Rng.Row).Resize(LRow - Rng.Row + 1) 

    End With 

    With Rng\_Elenchi.Cells 

        If Not UForm Is Nothing Then 

            vArr\_Temp = Rng\_Elenchi.Value 

            ReDim vArr(1 To UBound(vArr\_Temp)) 

            For i = 1 To .Count 

                If Not IsEmpty(.Item(i, 1)) Then 

                    iCtr = iCtr + 1 

                    vArr(iCtr) = vArr\_Temp(i, 1) 

                End If 

            Next i 

            With vArr\_Combo(Res - 1) 

                .List = vArr 

                .ListIndex = 0 

            End With 

        End If 

    End With 

End If 

End Sub

'<<========

Nel modulo di codice della Userform

'========>>

Option Explicit

Dim SH As Worksheet

'-------->>

Private Sub UserForm_Initialize()

Dim Rng As Range, Rng1 As Range, Rng2 As Range, Rng3 As Range 

Dim Rng4 As Range, Rng5 As Range, Rng6 As Range 

Dim vArr() As Variant, vArr\_Temp As Variant 

Dim vArr\_Prime\_Celle As Variant 

Dim vArr\_Elenchi() As Variant 

Dim i As Long, iCtr As Long 

Dim LRow As Long 

Dim UB As Long 

Dim CBox As MSForms.ComboBox 

Dim ii As Long 

Const sFoglio As String = **"Foglio1"                             '&lt;&lt;=== Modifica** 

Const iMax\_Numero\_Nomi = **50                                   '&lt;&lt;=== Modifica** 

Set SH = ThisWorkbook.Sheets(sFoglio) 

With SH 

    vArr\_Prime\_Celle = Split(sPrime\_Celle\_Elenchi, ",") 

    ReDim vArr\_Elenchi(1 To UBound(vArr\_Prime\_Celle) + 1) 

    Set Rng = .Range(vArr\_Prime\_Celle(0)) 

    LRow = LastRow(SH, Rng.EntireColumn) 

    With Rng 

        Set Rng1 = Rng.Resize(LRow - .Row + 1) 

    End With 

    Set Rng = .Range(vArr\_Prime\_Celle(1)) 

    LRow = LastRow(SH, Rng.EntireColumn) 

    With Rng 

        Set Rng2 = Rng.Resize(LRow - .Row + 1) 

    End With 

    Set Rng = .Range(vArr\_Prime\_Celle(2)) 

    LRow = LastRow(SH, Rng.EntireColumn) 

    With Rng 

        Set Rng3 = Rng.Resize(LRow - .Row + 1) 

    End With 

    Set Rng = .Range(vArr\_Prime\_Celle(3)) 

    LRow = LastRow(SH, Rng.EntireColumn) 

    With Rng 

        Set Rng4 = Rng.Resize(LRow - .Row + 1) 

    End With 

    Set Rng = .Range(vArr\_Prime\_Celle(4)) 

    LRow = LastRow(SH, Rng.EntireColumn) 

    With Rng 

        Set Rng5 = Rng.Resize(LRow - .Row + 1) 

    End With 

    Set Rng = .Range(vArr\_Prime\_Celle(5)) 

    LRow = LastRow(SH, Rng.EntireColumn) 

    With Rng 

        Set Rng6 = Rng.Resize(LRow - .Row + 1) 

    End With 

End With 

vArr\_Elenchi = VBA.Array(Rng1, Rng2, Rng3, Rng4, Rng5, Rng6) 

vArr\_Combo = VBA.Array(ComboBox1, ComboBox2, ComboBox3, ComboBox4, ComboBox5, ComboBox6) 

ReDim vArr\_Arrays(LBound(vArr\_Elenchi) To UBound(vArr\_Elenchi)) 

Set UForm = Me 

For ii = LBound(vArr\_Elenchi) To UBound(vArr\_Elenchi) 

    If ii = 3 Then Stop 

    vArr\_Temp = vArr\_Elenchi(ii).Value 

    UB = UBound(vArr\_Temp) 

    ReDim vArr(1 To UB) 

    For i = 1 To UB 

        With Rng.Cells 

            If Not IsEmpty(.Item(i, 1)) Then 

                iCtr = iCtr + 1 

                vArr(iCtr) = vArr\_Temp(i, 1) 

            End If 

        End With 

    Next i 

    vArr\_Arrays(ii) = vArr 

    ReDim Preserve vArr(1 To iCtr) 

    If CBool(iCtr) Then 

        Set CBox = vArr\_Combo(ii) 

        With CBox 

            .ListRows = iMax\_Numero\_Nomi 

            .List = vArr 

            .ListIndex = 0 

        End With 

    Else 

        Call MsgBox(Prompt:="Controlla l'intervallo dei nomi, " & Rng.Address(0, 0) & " in quanto pare che risulti vuota!", \_ 

            Buttons:=vbCritical, \_ 

            Title:="REPORT") 

        Exit Sub 

    End If 

    iCtr = 0 

    vArr\_Arrays(ii) = vArr 

Next ii 

End Sub

'-------->>

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

bFlag = False 

End Sub

'-------->>

Private Sub cbEsci_Click()

Unload Me 

End Sub

'<<========

Con questo codice, eventuali modifiche, aggiunte o eliminazioni apportate a una qualsiasi delle sei colonne di dati di interesse si riflettono automaticamente in tempo reale nei controlli ComboBox della Userform aperta.

Per testare il mio codice, ho utilizzato una configurazione dati del seguente tipo:

  [![Immagine](https://learn-attachment.microsoft.com/api/attachments/1925d696-c2c8-448d-9782-7267adfadd99?platform=QnA"https://1drv.ms/x/s!AmTW9HzZG8cql2P7yP1AGBIFXbWn?e=SixqVj" title="1drv.ms" rel="ugc nofollow">Geacs20231026.xlsm

===

Regards,

Norman

Immagine

La risposta è stata utile?

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

Risposta accettata dall'autore della domanda

Anonimo
2023-10-24T13:40:25+00:00

Ciao Geacs,

Leggermente più veloce, ed esteticamente più gradevole, sarebbe sostituire il seguente codice nella procedura Worksheet_Change

    If Not UForm Is Nothing Then 

        **Unload UForm** 

       **Call Avvia\_Userform** 

    End If 

con:

    If Not UForm Is Nothing Then 

        **UForm.ComboBox1.List = Rng\_Nomi.Value** 

    End If 

Ho adottato il primo approccio solo perché il secondo approccio (che è quello che ho usato invariabilmente in passato - anche nei tuoi file, se non ricordo male) è stato recentemente segnalato sporadicamente per causare problemi imprevisti. Tuttavia, avendo ampiamente ritestato il mio approccio storico, non sono stato in grado di replicare i problemi segnalati. Concludo quindi che tali problemi erano di natura transitoria (forse legati a un aggiornamento del software) e non vedo alcun motivo per non tornare all'approccio collaudato e affidabile.

===

Regards,

Norman

Immagine

La risposta è stata utile?

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

Risposta accettata dall'autore della domanda

Anonimo
2023-10-24T12:22:18+00:00

Ciao Geacs,

Ciao Norman,

Ho provato sia a togliere che ad aggiungere alcuni nomi, ma non si aggiorna. Dove sbaglio?

Ho scaricato il mio file da OneDrive

Per visualizzare e dimostrare adeguatamente i miei risultati, ho modificato la proprietà ListRows del tuo ComboBox da 8 a 30.

Ho fatto clic sul pulsante su Foglio1 per avviare la Userform

Facendo clic sul ComboBox, vedo elencati tutti i 20 nomi originali:

  [![Immagine](https://learn-attachment.microsoft.com/api/attachments/8bbb74b8-de29-4de4-a1c9-63cd665b3d08?platform=QnA"https://learn-attachment.microsoft.com/api/attachments/8dc06fb9-50c3-4c75-abd6-3280370e24bb?platform=QnA" title="filestore.community.support.microsoft.com" rel="ugc nofollow">Immagine

.

Ho ora aggiornato il mio file di prova Geacs2_20231023.xlsm

===

Regards,

Norman

Immagine

La risposta è stata utile?

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

15 risposte aggiuntive

Ordina per: Più utili
  1. Anonimo
    2023-10-23T13:09:03+00:00

    Ciao Geacs,

    Ho bisogno del vostro aiuto, per compilare mediante una combo box inserita in una Userfom le celle che trovate nell'esempio allegato. Devo poter usare l'elenco, iniziando da qualsiasi nominativo. Se il nominativo ad esempio, dovesse essere l'ultimo deve ripartire dal primo. Il file lo trovate qui.

    Nel modulo di codice dellla UIserform1, incolla il seguente codice:

    '========>>

    Option Explicit

    Dim SH As Worksheet

    Dim vArr As Variant

    '-------->>

    Private Sub UserForm_Initialize()

    Dim Rng As Range 
    
    Const sFoglio As String = **"Foglio1"                                           '&lt;&lt;=== Modifica**
    
    Const sElenco\_Nomi As String = **"Q2:Q21"                                 '&lt;&lt;=== Modifica**
    
    Set SH = ThisWorkbook.Sheets(sFoglio) 
    
    Set Rng = SH.Range(sElenco\_Nomi) 
    
    vArr = Rng.Value 
    
    With Me.ComboBox1 
    
    .List = vArr 
    
    .ListIndex = 0 
    
    End With 
    

    End Sub

    '-------->>

    Private Sub CommandButton1_Click()

    Dim Rng2 As Range 
    
    Dim i As Long, iName As Long 
    
    Dim iIndex As Long, iCtr As Long 
    
    Const sIntervallo\_Da\_Compilare As String = **"G3:G48"               '&lt;&lt;=== Modifica**
    
    Set Rng2 = SH.Range(sIntervallo\_Da\_Compilare) 
    
    iName = Me.ComboBox1.ListIndex + 1 
    
    iCtr = iName 
    
    For i = 1 To Rng2.Cells.Count Step 5 
    
        If iCtr &gt; UBound(vArr) Then iCtr = 1 
    
        Rng2.Cells(i).Value = vArr(iCtr, 1) 
    
        iCtr = iCtr + 1 
    
    Next i 
    

    End Sub

    '<<========

    Potresti scaricare il mio file di prova Geacs202631023.xlsm

    ===

    Regards,

    Norman

    Immagine

    La risposta è stata utile?

    0 commenti Nessun commento