VBA - ComboBox en de bijbehorende code toevoegen

Deze code is relatief eenvoudig en vereist geen verdere uitleg.

Plak deze twee sub in een algemene module (bijvoorbeeld Module1).

 Optie Expliciete Sub CréerCombo (Lier As Range, Optioneel Sh As werkblad, Optionele plaatsing als bereik) Dim Obj als object Dim code As String Dim Ix As Integer, Num As String Als Sh niets is Stel dan in Sh = ActiveSheet End If Sh.Select Ix = ActiveSheet.OLEObjects.Count + 1 Num = "0" & ​​Ix: Num = Right (Num, 2) 'Pour jusque 99 boutons If Emplacement Is Nothing Then' emplacement par défaut Set Emplacement = ActiveCell End If 'crée le bouton With Emplacement Stel Obj = ActiveSheet.OLEObjects.Add (ClassType: = "Forms.Combobox.1", _ Link: = False, DisplayAsIcon: = False, Left: =. Left, Top: =. Top, Width: =. ColumnWidth * 5.65, Height: =. RowHeight) Obj.Name = "ComboB_" & Num End With 'propriétés du combo Met ActiveSheet.OLEObjects (Ix) .Placement = xlMoveAndSize .PrintObject = True .ListFillRange = Lier.Address End With' Le texte de la macro code = vbCrLf & "Private Sub ComboB_" & Num & "_Change ()" & vbCrLf code = code & "MsgBox" "Vous avez sélectionner la ligne" "& ComboB_" & Num & ".Text,, " "ComboB_" & Num & "" "" & vbCrLf code = code & "End Sub" 'Ajoute la macro en fin de module feuille Met ActiveWorkbook.VBProject.VBComponents (ActiveSheet.Name) .CodeModule .insertlines .CountOfLines + 1, code End With End Sub 

Download een voorbeeld voor Excel 2007:

Download project:

  • Server 1: voeg Combo en code.xlsm toe
  • Private Server: voeg Combo en code.xlsm toe

Vorige Artikel Volgende Artikel

Top Tips