如何将列表框项添加到VBA中的列?

编程入门 行业动态 更新时间:2024-10-21 13:34:47
本文介绍了如何将列表框项添加到VBA中的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有2个列表框,如图所示.因此,在完成列表框2并单击``确定''后,应该将列表框2中的数据添加到范围A2向下.使用.AddItem将数据填充到列表框1中 .

I have 2 list boxes like in image.So when the listbox 2 is finalised and I click 'OK', the data in listbox2 should be added to range A2 to down. Data is populated in listbox 1 using .AddItem .

我尝试过这样:

Option Explicit Private Sub BTN_moveAllLeft_Click() Dim iCtr As Long For iCtr = 0 To Me.ListBox2.ListCount - 1 Me.ListBox1.AddItem Me.ListBox2.List(iCtr) Next iCtr Me.ListBox2.Clear End Sub Private Sub BTN_moveAllRight_Click() Dim iCtr As Long For iCtr = 0 To Me.ListBox1.ListCount - 1 Me.ListBox2.AddItem Me.ListBox1.List(iCtr) Next iCtr Me.ListBox1.Clear End Sub Private Sub BTN_MoveSelectedLeft_Click() Dim iCtr As Long For iCtr = 0 To Me.ListBox2.ListCount - 1 If Me.ListBox2.Selected(iCtr) = True Then Me.ListBox1.AddItem Me.ListBox2.List(iCtr) End If Next iCtr For iCtr = Me.ListBox2.ListCount - 1 To 0 Step -1 If Me.ListBox2.Selected(iCtr) = True Then Me.ListBox2.RemoveItem iCtr End If Next iCtr End Sub Private Sub BTN_MoveSelectedRight_Click() Dim iCtr As Long For iCtr = 0 To Me.ListBox1.ListCount - 1 If Me.ListBox1.Selected(iCtr) = True Then Me.ListBox2.AddItem Me.ListBox1.List(iCtr) End If Next iCtr For iCtr = Me.ListBox1.ListCount - 1 To 0 Step -1 If Me.ListBox1.Selected(iCtr) = True Then Me.ListBox1.RemoveItem iCtr End If Next iCtr End Sub Private Sub cmdOK_Click() Dim lngLastRow As Long Dim lngCol As Long Dim lngIndex As Long lngLastRow = Range("D" & Rows.Count).End(xlUp).Row + 1 lngCol = 4 For lngIndex = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(lngIndex) Then Cells(lngLastRow, lngCol) = ListBox2.List(lngIndex) lngCol = lngCol + 1 End If Next End Sub Private Sub UserForm_Initialize() Dim iCtr As Long With Me.ListBox1 For iCtr = 1 To 10 .AddItem "This is a test" & iCtr Next iCtr End With Me.ListBox1.MultiSelect = fmMultiSelectMulti Me.ListBox2.MultiSelect = fmMultiSelectMulti End Sub

推荐答案

如果您要填充范围A2中Listbox2中的数据,并在按下按钮OK时向下填充,请尝试以下操作:

If you want to populate data from Listbox2 in Range A2 and down once button OK is pressed, try this:

Private Sub cmdOK_Click() Dim lngLastRow As Long Dim lngCol As Long Dim lngIndex As Long For lngIndex = 0 To ListBox2.ListCount - 1 Cells(lngIndex + 2, 1).Value = ListBox2.List(lngIndex) Next End Sub

更多推荐

如何将列表框项添加到VBA中的列?

本文发布于:2023-11-23 15:57:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622038.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   列表   VBA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!