Ciao Francesco LE ROSE. (Ciao Norman!)
Francesco, ti eri espresso benissimo. Solo non hai eseguito gli esempi, di Norman e mio. Quando ti viene proposta una soluzione non basta leggerla, bisogna anche sperimentarla e eseguirla passo passo e ispezionare i valori che assumono le variabili. Solo
così si riesce a capire a fondo ciò che viene proposto.
Ora ti ripropongo il mio esempio, in due versioni con commenti, spero significativi. Falli girare in una
Cartella di lavoro nuova, con almeno due ComboBox.
' Module: Modulo1
'
Option Explicit
' Uso didattico. È evidenziata la gerarchia degli oggetti.
'
Public Sub Test1()
Const cstrWsh = "Foglio1"
Const cstrCboPrefix = "cb_"
Const cstrCboFormat = "00"
Dim wbk As Excel.Workbook
Dim wsh As Excel.Worksheet
Dim olef As Excel.OLEFormat
Dim oleo As Excel.OLEObject
Dim cbo As MSForms.ComboBox
Dim strCboName As String
Dim i As Long
Dim n As Long
Set wbk = Application.ThisWorkbook
Set wsh = wbk.Worksheets.Item(cstrWsh)
' Ora definisco 'dinamicamente' il nome di una ComboBox
' che nel tuo progetto si chiamano "cb_" + un numero
' di due cifre. In questo caso uso la numero 2:
'
i = 2
strCboName = cstrCboPrefix & Format$(i, cstrCboFormat)
Debug.Print "'"; strCboName; "'"
Set olef = wsh.Shapes.Item(strCboName).OLEFormat
Set oleo = olef.Object
Set cbo = oleo.Object
n = 5
oleo.ListFillRange = "B3:C" & n
Debug.Print "'"; oleo.ListFillRange; "'"
Set cbo = Nothing
Set oleo = Nothing
Set olef = Nothing
Set wsh = Nothing
Set wbk = Nothing
End Sub
' Uso normale.
'
Public Sub Test2()
Const cstrWsh = "Foglio1"
Const cstrCboPrefix = "cb_"
Const cstrCboFormat = "00"
Dim wbk As Excel.Workbook
Dim wsh As Excel.Worksheet
Dim oleo As Excel.OLEObject
Dim strCboName As String
Dim i As Long
Dim n As Long
Set wbk = Application.ThisWorkbook
Set wsh = wbk.Worksheets.Item(cstrWsh)
' Ora definisco... (Vedi commento in Test1)
'
i = 2
strCboName = cstrCboPrefix & Format$(i, cstrCboFormat)
Debug.Print "'"; strCboName; "'"
Set oleo = wsh.Shapes.Item(strCboName).OLEFormat.Object
n = 5
oleo.ListFillRange = "B3:C" & n
Debug.Print "'"; oleo.ListFillRange; "'"
Set oleo = Nothing
Set wsh = Nothing
Set wbk = Nothing
End Sub