Ciao Geacs,
Credo che i due problemi che hai sollevato sono entrambi a causa del modo in cui hai tentato di adattare il mio ultimo codice per sfruttare una parte dell'approccio che ho utilizzato nella seconda versione del codice pubblicato da me.
Credo comunque che si possa risolvere i problemi utilizzando il seguente modifica del codice incluso nell'ultimo file caricato da te, in cui le mie modifiche sono evidenziate in grassetto:
'=========>>
Option Explicit
Dim arrCodici As Variant
Dim arrProdotti As Variant
Dim arrDescrizioni 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:B30" '\ Una singola colonna!!
Set WB = ThisWorkbook
Set SH_Codice = WB.Sheets(sFoglio)
Set rngCodici = SH_Codice.Range(sIntervalloCodici)
arrCodici = rngCodici.Value
arrProdotti = rngCodici.Offset(0, 1).Value
arrDescrizioni = rngCodici.Columns(2).Value
With Me.cbxCodici
.ColumnCount = 2
.List = arrCodici
.ListIndex = -1
End With
End Sub
'--------->>
Private Sub cbRegistraCodice_Click()
Dim Sh As Worksheet
Dim shI 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 sDescrizione As String
Dim i As Long, iCtr As Long, iMatch As Long
Dim FirstRow As Long, LRow As Long
Dim lRiga As Long
Dim bForbidden As Boolean
Dim bValidSheet As Boolean
Set shI = ThisWorkbook.Worksheets("Foglio1")
With Application
bValidSheet = Not IsError(.Match(ActiveSheet.Name, _
.GetCustomListContents(4), 0))
End With
If Not bValidSheet Then
Call MsgBox( _
Prompt:="Questa procedura può essere eseguita " _
& "solo su un foglio mensile!", _
Buttons:=vbCritical, _
Title:="FOGLIO NON VALIDO!")
Exit Sub
End If
Const sIntervallo As String = "E6:E60"
Const sPassword As String = "Pippo"
Const iMaxCodici As Long = 5
sNuovoCodice = Me.cbxCodici.Value
Set Sh = ActiveSheet
lRiga = Sh.Range("A60").End(xlUp).Row + 1
On Error Resume Next
With Me
With Sh
If IsNumeric(TextBox2.Value) And TextBox2.Value > 0 Then
.Range("A" & lRiga).Value = TextBox1.Text
.Range("B" & lRiga).Value = shI.Range("B2").Value
.Range("E" & lRiga).Value = "A"
.Range("F" & lRiga).Value = CDbl(TextBox2.Text)
lRiga = lRiga + 1
End If
If IsNumeric(TextBox3.Value) And TextBox3.Value > 0 Then
.Range("A" & lRiga).Value = TextBox1.Text
.Range("B" & lRiga).Value = shI.Range("B3").Value
.Range("E" & lRiga).Value = "B"
.Range("F" & lRiga).Value = CDbl(TextBox3.Text)
lRiga = lRiga + 1
End If
If IsNumeric(TextBox4.Value) And TextBox4.Value > 0 Then
.Range("A" & lRiga).Value = TextBox1.Text
.Range("B" & lRiga).Value = shI.Range("B4").Value
.Range("E" & lRiga).Value = "C"
.Range("F" & lRiga).Value = TextBox4.Text
lRiga = lRiga + 1
End If
If IsNumeric(TextBox5.Value) And TextBox5.Value > 0 Then
.Range("A" & lRiga).Value = TextBox1.Text
.Range("B" & lRiga).Value = shI.Range("B5").Value
.Range("E" & lRiga).Value = "D"
.Range("G" & lRiga).Value = CDbl(TextBox5.Text)
lRiga = lRiga + 1
End If
If IsNumeric(TextBox6.Value) And TextBox6.Value > 0 Then
.Range("A" & lRiga).Value = TextBox1.Text
.Range("B" & lRiga).Value = shI.Range("B6").Value
.Range("E" & lRiga).Value = "E"
.Range("G" & lRiga).Value = CDbl(TextBox6.Text)
lRiga = lRiga + 1
End If
End With
End With
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)
iMatch = Application.Match(arrUniqueCodes(i), arrCodici, 0)
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
End With
End If
End If
If Not bForbidden Then
With Me.cbxCodici
rngDestinazione.Value = .List(.ListIndex, 0)
rngDestinazione.Offset(0, -3).Value = arrProdotti(i.ListIndex + 1, 1)
rngDestinazione.Offset(0, 1).Value = Me.TextBox7.Value
rngDestinazione.Offset(0, -4).Value = Me.TextBox1.Value
.ListIndex = -1
End With
End If
With Me
.TextBox1.Text = ""
.TextBox2.Text = ""
.TextBox3.Text = ""
.TextBox4.Text = ""
.TextBox5.Text = ""
.TextBox6.Text = ""
.TextBox7.Text = ""
.cbxCodici = ""
End With
With Me
.TextBox1.SetFocus
End With
End Sub
'<<=========
Potresti scaricare il mio file di prova Geacs20180129.xlsm
===
Regards,
Norman
