VBA ListBox selected item

QasimMGM

New Member
Joined
Nov 18, 2020
Messages
11
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi,
I,m new to VBA and started my first user form with buttons and list box, I wrote the code below to open a file in my laptop that named after the first item in the selected line in the Listbox:

Private Sub CmdFindLetter_Click()
Dim i As String
i = ListBox1.Selected(0)
ThisWorkbook.FollowHyperlink "D:\Tests\Letters\L-" + i + ".pdf"
111.jpg
111.jpg
111.jpg
111.jpg
111.jpg
111.jpg

End Sub


but I got this message (image attached)

Any solution?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Welcome to MrExcel
In future please click on VBA icon and then paste your code inside the code tags which appear

[ CODE=vba ] paste your code inside the tags [ /CODE ]

If the code requires highlighting ( as I have done) click on RICH icon and paste your code between the tags which appear

This method works for me
Rich (BB code):
Private Sub CmdFindLetter_Click()
    Dim i As String
    i = ListBox1.Text
    ThisWorkbook.FollowHyperlink "D:\Tests\Letters\L-" & i & ".pdf"
End Sub
 
Upvote 0
Solution
Welcome to MrExcel
In future please click on VBA icon and then paste your code inside the code tags which appear

[ CODE=vba ] paste your code inside the tags [ /CODE ]

If the code requires highlighting ( as I have done) click on RICH icon and paste your code between the tags which appear

This method works for me
Rich (BB code):
Private Sub CmdFindLetter_Click()
    Dim i As String
    i = ListBox1.Text
    ThisWorkbook.FollowHyperlink "D:\Tests\Letters\L-" & i & ".pdf"
End Sub
Thank you very very much..
This works for me.
I really appreciate your help..
further question if you allow.. this code will take the contents of the first column of the selected item of the list.. what if I want the 2nd or 3rd item..?
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top