Excel VBA 拖放

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16194208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:49:17  来源:igfitidea点击:

Excel VBA Drag and Drop

excelvba

提问by Overseer10

I'm having some difficulty finding a workable solution (been looking for 2 days now). Hopefully you can help me figure it out.

我在寻找可行的解决方案时遇到了一些困难(现在已经找了 2 天了)。希望你能帮我弄清楚。

Purpose- I'm trying to use VBA to drag and drop text between listboxes (see image)

目的- 我正在尝试使用 VBA 在列表框之间拖放文本(见图)

enter image description here

在此处输入图片说明

Note: I know there are Pivot Wizards already, I'm not so interested in them (long story)

注意:我知道已经有 Pivot Wizards,我对它们不太感兴趣(长话短说)

QuestionIs there any solution that you know of that could help me move "Column A" to any of the other listbox?

问题是否有任何您知道的解决方案可以帮助我将“A 列”移动到任何其他列表框?

If you don't know of a solution, a blog or site might be helpful as well.

如果您不知道解决方案,博客或网站也可能会有所帮助。

回答by Siddharth Rout

Further to my comments above here is the most simple way to do it.

除了我上面的评论之外,这里是最简单的方法。

Create a Userform with 2 Listboxes and 1 Command Button as shown in the below image.

创建一个包含 2 个列表框和 1 个命令按钮的用户窗体,如下图所示。

enter image description here

在此处输入图片说明

And paste this code in the Userform Code area

并将此代码粘贴到用户表单代码区域中

Dim i As Long

Private Sub UserForm_Initialize()
    For i = 1 To 10
        ListBox1.AddItem i
    Next i
End Sub

Private Sub CommandButton1_Click()
    If ListBox1.ListIndex = -1 Then
        MsgBox "Please select an item from listbox1"
        Exit Sub
    End If

    ListBox2.AddItem ListBox1.List(ListBox1.ListIndex)
    ListBox1.RemoveItem (ListBox1.ListIndex)
End Sub

HTH

HTH