因此,我在论坛(包括堆栈)之后搜索了谷歌和论坛,试图解决这个问题。我想要做的就是有一个 ActiveX 按钮将列表框的内容导出到 Excel 中的某个范围。
下面是我将项目从 ListBox1 添加到 ListBox2 的代码。在所有需要的项目都移动到 ListBox2 之后,所述 ActiveX 按钮 (SomeButton_Click) 会将 ListBox2 中的所有项目导出到从 range(“a1”) 开始的“Sheet15”。
(请注意,这是一个不在实际表单上的 ActiveX 列表框。它在工作表中)
Public Sub BTN_MoveSelectedRight_Click()
Dim iCtr As Long
Dim n As Long, lRow As Long
Dim cStartCell As Range
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
下面是执行导出的按钮:
Public Sub SomeButton_Click()
'What code can I put here to perform the export to abovementioned range?
End sub
任何帮助都将不胜感激,因为我花了几个小时试图弄清楚这一点(尽管我不想真正承认这一点,这是真的)
谢谢!
最佳答案
干得好:
Public Sub SomeButton_Click()
Dim v
v = Sheet15.ListBox2.List
Sheet15.[a1].Resize(UBound(v)) = v
End Sub
如果列表框位于不同的工作表上,则需要对其进行调整。
编辑#1
这个更好:
Public Sub SomeButton_Click()
With Sheet15
.[a1].Resize(.ListBox1.ListCount) = .ListBox1.List
End With
End Sub
关于vba - 将列表框中的项目导出到 Excel 中的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32130766/