Ciao Geacs,
Ciao Norman,
La seconda alternativa è quella corretta. Anche se parto dalla secondo, terzo o ultimo valore della combobox, devono essere riportati tutti i valori che visualizzo nella combobox, senza saltare nessuna. L'esigenza parte dal fatto che il foglio1 che sarà compilato inizia in quel caso dalla prima riga A5:I5 con il codice che vado a inserire.
Prova qualcosa del genere:
In un modulo standard, incolla:
'========>>
Option Explicit
'-------->>
Public Sub Show_Userform()
UserForm1.Show vbModeless
End Sub
'<<========
Nel modulo di codice della Usrrform, incolla il seguente codice:
'========>>
Option Explicit
Dim SH_Sorgente As Worksheet, SH_Destinazione As Worksheet
Dim srcRng As Range, destRng As Range
Dim iRow As Long, jRow As Long
Const sFoglio_Sorgente As String = "Foglio2" '<<=== Modifica
Const sFoglio_Destinazione As String = "Foglio1" '<<=== Modifica
Const iPrimaRiga_Sorgente As Long = 2 '<<=== Modifica
Const iPrimaRiga_Destinazione As Long = 5 '<<=== Modifica
'-------->>
Private Sub UserForm_Initialize()
With ThisWorkbook
Set SH\_Sorgente = .Sheets(sFoglio\_Sorgente)
Set SH\_Destinazione = .Sheets(sFoglio\_Destinazione)
End With
With SH\_Sorgente
iRow = LastRow(SH\_Sorgente, .Columns("P"))
Set srcRng = .Range("P" & iPrimaRiga\_Sorgente & ":P" & iRow)
End With
With SH\_Destinazione
jRow = LastRow(SH\_Sorgente, .Columns("A"))
.Range("I" & iPrimaRiga\_Destinazione & ":I" & jRow).ClearContents
Set destRng = .Range("I5")
End With
With srcRng
Me.ComboBox1.List = .Value
Me.ComboBox1.ListRows = .Cells.Count
End With
End Sub
'-------->>
Private Sub ComboBox1_Change()
Dim i As Long, j As Long, k As Long, p As Long, q As Long
With Me.ComboBox1
ReDim vArrIn(1 To .ListCount, 1 To 1)
If .ListIndex = -1 Then GoTo XIT
For i = .ListIndex To .ListCount - 1 '- .ListIndex + 1
p = p + 1
vArrIn(p, 1) = .List(i)
Next i
For j = 0 To .ListIndex - 1
p = p + 1
vArrIn(p, 1) = .List(j)
Next j
End With
For k = iPrimaRiga\_Destinazione To jRow - iRow + 2 Step iRow - 1
destRng.Offset(k - iPrimaRiga\_Destinazione).Resize(iRow - 1).Value = vArrIn
Next k
Exit Sub
XIT:
destRng.Resize(jRow - 5 + 1).ClearContents
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 < minRow Then
LastRow = minRow
End If
End Function
'<<========
Potresti scaricare il mio file di prova Geacs20240103.xlsm
A causa di un problema con l'attuale editor del forum, che inserisce righe vuote indesiderate nel codice copiato dal forum, suggerirei di copiare il mio codice direttamente dal mio file di prova.
Inoltre, come potrebbe essere possibile, hai già una funzione Ultima riga nella tua cartella di lavoro, potresti eliminare questa procedura dal codice sopra.
===
Regards,
Norman
