首页 > 解决方案 > 将列表框中的项目添加到另一个 VBA

问题描述

我有以下列表,我需要从列表“Liste de toute les lames”中选择一个项目,然后单击“Ajouter lames enService”按钮,该项目将添加到“Lames en Service”列表中:

在此处输入图像描述

编码 :

Private Sub Ajout_Lame_S_Click()
Dim ws_Lame As Worksheet
Dim Modele As String
Dim dl As Integer
Modele = "Liste_Lame_" & Me.TextBox1.Value
If Me.ListBox_Lames.ListIndex = -1 Then
    MsgBox ("Veuillez choisir une lame de la liste de toutes les lames")
Else
    Set ws_Lame = ActiveWorkbook.Worksheets(Modele)
    dl = ws_Lame.Range("A65530").End(xlUp).Row
    ws_Lame.Activate
    Nom_LamesS = Me.ListBox_Lames.Value
    ws_Lame.Cells(dl + 1, 5) = Nom_LamesS
    'Mettre a jour la liste
    For i = 2 To dl
        Me.ListBox_LamesS.AddItem ws_Lame.Range("E" & i)
    Next
End If
MsgBox ("Lame ajoutée a la liste lames en Service!")
End Sub

我不知道我的代码有什么问题,但我的目标列表中没有添加任何内容。

标签: excelvba

解决方案


从字面上理解你的问题,如果你想将一行从一个列表框复制到另一个,你需要使用的代码是这个

Private Sub CommandButton1_Click()
    
    Dim I As Long
    
    With Me.ListBox1
        If .ListIndex <> -1 Then
            Me.ListBox2.AddItem
            For I = 0 To .ColumnCount - 1
                Me.ListBox2.List(Me.ListBox2.ListCount - 1, I) = .List(.ListIndex, I)
            Next I
        End If
    End With

End Sub

推荐阅读