Ciao Geacs,
Favoloso, eccezionale hai preso in pieno la mia domanda. Se non ti chiedo troppo, potresti aggiungere accanto ai codici che visualizzo nella msgbox anche il valore riportato nelle celle accanto da C20:C30? Esempio al codice A001 Prodotto1
Sostituisci il codice nel modulo di codice della Userform con la seguente versione in cui le modifiche sono evidenziate in grassetto:
'=========>>
Option Explicit
Dim arrCodici As Variant
'--------->>
Private Sub UserForm_Initialize()
Dim WB As Workbook
Dim SH As Worksheet, SH_Codice As Worksheet
Dim rngCodici As Range
Const sFoglio As String = "Foglio1"
Const sIntervalloCodici As String = "B20:C30"
Set WB = ThisWorkbook
Set SH_Codice = WB.Sheets(sFoglio)
Set rngCodici = SH_Codice.Range(sIntervalloCodici)
arrCodici = rngCodici.Value
With Me.cbxCodici
.ColumnCount = 2
.List = arrCodici
.ListIndex = 0
End With
End Sub
'--------->>
Private Sub cbRegistraCodice_Click()
Dim SH As Worksheet
Dim RngDisponibile As Range, RngPopolato As Range
Dim rngDestinazione As Range
Dim arrMovimenti As Variant
Dim arrUniqueCodes As Variant
Dim arrCodiceUtilizzati() As Variant
Dim Res As Variant, Res2 As Variant
Dim sNuovoCodice As String, sList As String
Dim i As Long, iCtr As Long
Dim FirstRow As Long, LRow As Long
Dim bForbidden As Boolean
Const sIntervallo As String = "E6:E60"
Const sPassword As String = "Pippo"
Const iMaxCodici As Long = 5
sNuovoCodice = Me.cbxCodici.Value
Set SH = ActiveSheet
With SH
Set RngDisponibile = .Range(sIntervallo)
FirstRow = RngDisponibile.Row
LRow = LastRow(SH, RngDisponibile, , sPassword)
End With
If LRow < FirstRow Then
Set rngDestinazione = RngDisponibile.Cells(1)
Else
Set rngDestinazione = RngDisponibile.Cells(LRow - FirstRow + 2)
End If
If LRow > iMaxCodici + FirstRow Then
Set RngPopolato = RngDisponibile. _
Resize(LRow - RngDisponibile.Row + 1)
arrUniqueCodes = SortedUniqueList(RngPopolato.Value)
If UBound(arrUniqueCodes) >= iMaxCodici Then
For i = 1 To UBound(arrUniqueCodes)
Res = Application.Match(arrUniqueCodes(i), arrCodici, 0)
If Not IsError(Res) Then
iCtr = iCtr + 1
ReDim Preserve arrCodiceUtilizzati(1 To iCtr)
arrCodiceUtilizzati(iCtr) = arrUniqueCodes(i)
End If
Next i
End If
End If
If iCtr >= iMaxCodici Then
sNuovoCodice = Me.cbxCodici.Value
Res2 = Application.Match(sNuovoCodice, arrUniqueCodes, 0)
If IsError(Res2) Then
bForbidden = True
sList = Join(arrCodiceUtilizzati, vbNewLine)
Call MsgBox( _
Prompt:="Non puoi registrare il codice " _
& vbNewLine & sNuovoCodice & vbNewLine _
& "perchè il massimo numero di codice utilizzabile è " _
& iMaxCodici _
& vbNewLine & vbNewLine _
& "i codice già registrati sono:" _
& vbNewLine _
& sList, _
Buttons:=vbCritical, _
Title:="REPORT")
With Me.cbxCodici
.ListIndex = -1
.SetFocus
End With
End If
End If
If Not bForbidden Then
' \ Tutto a posto! Quindi procediamo la registrazione
'\ dei dati, ad sempio:
With Me.cbxCodici
rngDestinazione.Value = .List(.ListIndex, 0)
rngDestinazione.Offset(0, 1).Value = .List(.ListIndex, 1)
.ListIndex = -1
End With
End If
End Sub
'<<=========
Ho aggiornato il mio file di prova Geacs20180125.xlsm
===
Regards,
Norman
