excel - 将列表框中的项目添加到另一个 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
我不知道我的代码有什么问题,但我的目标列表中没有添加任何内容。
解决方案
从字面上理解你的问题,如果你想将一行从一个列表框复制到另一个,你需要使用的代码是这个
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
推荐阅读
- xcode - SwiftUI:.fileExporter 仅导出 1 个文档
- ios - swift UIDatePicker 限制日期范围选择
- c++ - c++ 代码没有给出输出(主题:递归和二维向量)
- sql - 如何从oracle sql中的1行中的多列中获取不同的值
- php - 将冒号分隔的 key:value 数组转换为键控数组
- python - 在 python 中实现类似 golang 的通道结构
- c# - 我应该将 DBContext 注入内部类吗?
- url - 有没有办法在邮递员重定向后获取 URL?
- prolog - prolog 中的分辨率求解器
- javascript - 在维护顶部组件的状态时避免 React Hooks 重新渲染?