
da riportare in un modulo:
Sub Elenco_a_discesa()
Dim ListaNomi As String
Dim ListaFogli As String
Dim Foglio As Object
Range("a1").Select
ActiveWorkbook.Names.Add Name:="ListaNomi", RefersToR1C1:="='Foglio1'!R1"
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="=ListaNomi"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
For Each Foglio In Sheets
ListaFogli = ListaFogli & Foglio.Name & ","
Next Foglio
Range("a3").Select
ListaFogli = Left(ListaFogli, Len(ListaFogli) - 1)
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:=ListaFogli
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub
|
Sub Elenco_a_discesa()
Dim intervallo As Range
Dim elenco As New Collection
Dim Listaelenco
Dim c As Integer
Dim n As Integer
Dim ListaFogli As String
Dim Foglio As Object
'memorizzo i valori della prima riga del Foglio1 per crearmi l'elenco
With Worksheets("Foglio1").Activate
Set intervallo = Range(Cells(1, 1), Cells(1, 1).End(xlToRight))
fine = intervallo.Columns.Count
For c = 1 To fine
elenco.Add Cells(1, c).Text
Next
End With
'assegno un nome all'elenco
For n = 1 To fine ' o elenco.Count
Listaelenco = Listaelenco & elenco(n) & ","
Next n
'nel foglio2 creo l'elenco a discesa
Worksheets("Foglio2").Select
With Range("a1").Validation
.Delete
.Add Type:=xlValidateList, _
Formula1:=Listaelenco
End With
'nello stesso foglio creo l'elenco a discesa con i nomi di tutti i fogli
Range("a3").Select
For Each Foglio In Sheets
ListaFogli = ListaFogli & Foglio.Name & ","
Next Foglio
ListaFogli = Left(ListaFogli, Len(ListaFogli) - 1)
With Selection.Validation
.Delete
.Add Type:=xlValidateList, _
Formula1:=ListaFogli
End With
End Sub
|
Private Sub elenco_a_discesa2()
Dim ac As Range, lista_elenco As Range, choices As String, sh As Worksheet
'si suppone di avere un elenco di nomi dalla cella A2 in giù
'tipo pippo, pluto, topolino
'in A1 c'è l'intestazione di colonna, non serve ma è bello metterla ;)
Set lista_elenco = [a1].CurrentRegion.Offset(1, 0).Resize([a1].CurrentRegion.Rows.Count - 1, 1)
For Each ac In lista_elenco
choices = choices & ac & ","
Next
choices = Replace(choices & "@", ",@", "")
'nella cella C1 ritrovo l'elenco in cella dei nomi immessi dalla cella A2 in giù
With [c1].Validation
.Delete
.Add Type:=xlValidateList, Formula1:=choices
End With
'nella cella E1 ritrovo l'elenco dei fogli della cartella di lavoro
[e1].Validation.Delete
choices = ""
For Each sh In Sheets
choices = choices & sh.Name & ","
Next
choices = Replace(choices & "@", ",@", "")
[e1].Validation.Add Type:=xlValidateList, Formula1:=choices
End Sub
|
