Excel VBA 列表框链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23110442/
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-12 02:47:18  来源:igfitidea点击:

Excel VBA Listbox links

excelvbaexcel-vbaexcel-2007excel-2010

提问by HumanlyRespectable

I currently have a macro with two columns and many rows. The two columns holds info such as First name and a link to a folder or website.

我目前有一个包含两列多行的宏。两列包含诸如名字和文件夹或网站链接等信息。

When i click the button on the macro, it takes all the info from the excel sheet and shows the first name and places it in a listbox.

当我单击宏上的按钮时,它会从 Excel 工作表中获取所有信息并显示名字并将其放置在列表框中。

I was wondering, is it possible that when i click the button, i displays the first name in the listbox but also stores a link? when i select an item in the listbox, i want it to open up the link. is this possible?

我想知道,是否有可能当我单击按钮时,我会在列表框中显示名字但同时还存储了一个链接?当我在列表框中选择一个项目时,我希望它打开链接。这可能吗?

i have thought of one way, and that is with the listbox and an array which stores the link, and when i click on an item, it searches the array and then opens the link, FOR EXMAPLE: if i click the first item in the listbox, it will go into the array and go to array(1) and then get that link.

我想到了一种方法,那就是使用列表框和一个存储链接的数组,当我单击一个项目时,它会搜索该数组,然后打开链接,例如:如果我单击列表框,它将进入数组并转到数组(1),然后获取该链接。

That is one way i thought of but is there an easier way? rather than i storing the link into an array and all that.

这是我想到的一种方法,但有更简单的方法吗?而不是我将链接存储到一个数组中等等。

the current code that i have is:

我拥有的当前代码是:

For row = 3 To 10
    ListBox1.AddItem Range("A" & row).Text
Next

i don't know how to add a hyperlink to this code

我不知道如何向此代码添加超链接

采纳答案by Dmitry Pavliv

Update:

更新:

What I would do is create Listboxwith two columns:

我要做的是Listbox用两列创建:

Private Sub UserForm_Initialize()
    Dim row As Integer

    ListBox1.ColumnCount = 2
    ListBox1.ColumnWidths = "50;150"
    For row = 3 To 10
        ListBox1.AddItem Range("A" & row).Text
        ListBox1.List(ListBox1.ListCount - 1, 1) = Range("B" & row).Text
    Next
End Sub

Here is ListBox1_DblClickhandler (when user double clicked on listbox item):

这是ListBox1_DblClick处理程序(当用户双击列表框项时):

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    On Error GoTo ErrHandler
    ThisWorkbook.FollowHyperlink Address:=ListBox1.List(ListBox1.ListIndex, 1)
ExitHere:
    Exit Sub
ErrHandler:
    If Err.Number = -2147221014 Then
        MsgBox "Wrong link!"
    Else
        MsgBox "Error: " & Err.Description
    End If
    Resume ExitHere
End Sub

Then you can double click on any item in listbox to follow hyperlink:

然后您可以双击列表框中的任何项目以跟随超链接:

enter image description here

在此处输入图片说明

Also I suggest you to change Range("A" & row).Textto ThisWorkbook.Worksheets("Sheet1").Range("A" & row).Text

此外,我建议你改变Range("A" & row).TextThisWorkbook.Worksheets("Sheet1").Range("A" & row).Text