Private Sub Workbook_Open()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim LastRow As Long
LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
Dim iRow As Long
For iRow = 2 To LastRow
AddListValidation ws.Cells(iRow, "C"), ws.Cells(iRow, "B")
Next iRow
End Sub
Sub AddListValidation(cellSource As Range, cellTarget As Range)
cellTarget.Value = "Select your values here"
With cellTarget.Validation
.Delete
.Add Type:=xlValidateList, Formula1:=cellSource.Value
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub
La première fois que la liste déroulante est remplie. Quand je fais quelques changements sur la feuille et quand je rouvre à nouveau la feuille, il dit que c'est corrompu et que je ne reçois pas la liste déroulante également.
quelqu'un peut-il m'aider?
La formule que j'ai utilisée est correcte ou non?
.Add Type:=xlValidateList, Formula1:=cellSource.Value
Réponses:
0 pour la réponse № 1Très probablement le cellSource
est vide et donc il donne une erreur à Formula1
, qui tente d’obtenir sa valeur.
Écrivez votre code après le Sub AddListValidation
cette ligne:
If Len(cellSource) Then Exit Sub
Ainsi, il va sortir, si le cellSource
est vide et évitera l'erreur.