简体   繁体   English

Excel VBA列表框链接

[英]Excel VBA Listbox links

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. 我想到了一种方法,那就是列表框和存储链接的数组,当我点击一个项目时,它会搜索数组,然后打开链接,FOR EXMAPLE:如果我单击第一个项目列表框,它将进入数组并转到数组(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 我不知道如何添加到此代码的超链接

Update: 更新:

What I would do is create Listbox with 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_DblClick handler (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: 然后,您可以双击列表框中的任何项目以关注超链接:

在此输入图像描述

Also I suggest you to change Range("A" & row).Text to ThisWorkbook.Worksheets("Sheet1").Range("A" & row).Text 另外我建议你改变Range("A" & row).TextThisWorkbook.Worksheets("Sheet1").Range("A" & row).Text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM