Consulting

Results 1 to 5 of 5

Thread: Populate Hyperlinks in ListBox

  1. #1
    VBAX Regular
    Joined
    Aug 2013
    Posts
    16
    Location

    Populate Hyperlinks in ListBox

    Hello,
    I am new to programming, I need this codes results in ListBox how to do that?

    Sub HyperlinksToDirectory()
        Dim stDir As String
        Dim stFile As String
        Dim R As Range
        Set R = ActiveCell
        stDir = InputBox("Directory?", , Default:=CurDir())
        stFile = Dir(stDir & "\*.*")
        Do Until stFile = ""
            R.Hyperlinks.Add R, stDir & "\" & stFile, , , stFile
            Set R = R.Offset(1)
            stFile = Dir()
        Loop
        R.CurrentRegion.Sort key1:=R, order1:=xlAscending, Header:=xlNo
    End Sub

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    A ListBox can't hold hyperlinks.

    What exactly are you trying to accomplish?

    It looks like you're trying to populate a ListBox with file names and you want it to Open the selected file when clicked.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Regular
    Joined
    Aug 2013
    Posts
    16
    Location
    Quote Originally Posted by SamT View Post
    A ListBox can't hold hyperlinks.

    Okay, i was not knowing that a listox can't hold hyperlinks.

    What exactly are you trying to accomplish?
    It looks like you're trying to populate a ListBox with file names and you want it to Open the selected file when clicked.
    Yes, I want to populate the files names on ListBox from a particular folder, & if possible when clicked the selected file it opens.

    Thanks.

  4. #4
    VBAX Regular
    Joined
    Jan 2013
    Location
    House
    Posts
    13
    Location
    Hi,

    In a module include your procedure:
    Sub HyperlinksToDirectory()
        Cells(1, 1).Select
        Dim stDir As String
        Dim stFile As String
        Dim R As Range
        Set R = ActiveCell
        stDir = InputBox("Directory?", , Default:=CurDir())
        stFile = Dir(stDir & "\*.*")
        Do Until stFile = ""
            R.Hyperlinks.Add R, stDir & "\" & stFile, , , stFile
            Set R = R.Offset(1)
            stFile = Dir()
        Loop
        R.CurrentRegion.Sort key1:=R, order1:=xlAscending, Header:=xlNo
    End Sub
    
    
    Sub Avvia()
      UserForm1.Show
    End Sub
    In Userform add:
    Private Sub UserForm_Initialize()
      Sheets("Foglio1").Activate
      UserForm1.ListBox1.RowSource = ""
      UserForm1.ListBox1.RowSource = "a1:a" & Range("A" & Rows.Count).End(xlUp).Row
    End Sub
    
    
    Private Sub ListBox1_Change()
        If ListBox1.ListIndex < 0 Then Exit Sub
        On Error Resume Next
        Range(ListBox1.RowSource).Cells(ListBox1.ListIndex + 1, 1).Hyperlinks(1).Follow
        Unload Me
        On Error GoTo 0
    End Sub

  5. #5
    VBAX Regular
    Joined
    Aug 2013
    Posts
    16
    Location
    Thankyou, it works perfectly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •