Thank you for your reply. I appologize that I posted ListBox instead of
ComboBox.
Here's the code I have in a module to search a directory and list jpg
filenames and then create hyperlinks. And in my UserForm2 I have a ListBox1
and I can see the list of filenames from the Filenames worksheet if I
hardcode "Filenames!A1:A2588" in the RowSource properties, but I dont want to
hardcode it as the list of jpg file names will grow.
How and where do I put the vba code to get the RowSource?
Public Sub ListFilenames()
'=========================================================
'Initialize variables
'=========================================================
Dim Directory As String
Dim FileName As String
Dim IndexSheet As Worksheet
Dim rw As Long
Dim LastRow As Long
Dim picCnt As Integer
picCnt = 0
'=========================================================
'Activate Filenames worksheet
'=========================================================
ThisWorkbook.Worksheets("Filenames").Activate
Set IndexSheet = ThisWorkbook.ActiveSheet
'=========================================================
'Delete columns A and B
'=========================================================
IndexSheet.Columns("A:B").Delete Shift:=xlToLeft
'=========================================================
'Change the directory below as needed
'=========================================================
Directory = "N:\Parts\"
If Left(Directory, 1) <> "\" Then
Directory = Directory & "\"
End If
FileName = Dir(Directory & "*.jpg")
'=========================================================
'Populate column A with filenames
'=========================================================
rw = 1
Do While FileName <> ""
IndexSheet.Cells(rw, 1).Value = FileName
rw = rw + 1
FileName = Dir
picCnt = picCnt + 1
Loop
'=========================================================
'Find the row number of the last record
'=========================================================
LastRow = Worksheets("Filenames").Range("A65536").End(xlUp).Row
'=========================================================
'Create Hyperlinks and also a Named Range for the RowSource
'=========================================================
With Worksheets("Filenames")
With Range("B1")
.FormulaR1C1 = "=HYPERLINK(""N:\Parts\""&RC[-1])"
.AutoFill Destination:=Range("B1:B" & LastRow)
End With
End With
'=========================================================
'Format worksheet and wrapup
'=========================================================
Columns("A:B").EntireColumn.AutoFit
MsgBox "Number of pics: " & picCnt, vbOKOnly
'=========================================================
'Clean up
'=========================================================
Set IndexSheet = Nothing
End Sub
Thank you again for your reply.
--
Other programming languages I''''ve used are: Cobol, C++, Informix Database,
and Unix.
FSt1 said:
hi
something like this might work. I tested on a sheet list box which uses
listfillrange.
a forms listbox uses row source so change that in the code. i also used
generic sheet names (just for test) it should work for the forms also. you
may have to tweek it some since i did you a sheet listbox but the basic
sentax is there.
Sub testlist()
Dim lr As Long
Dim r As Range
'find last row on other sheet
lr = Sheets("sheet2").Cells(Rows.Count, "A").End(xlUp).Row
'set the range
Set r = Range("A2:A" & lr)
'set the listfillrange(rowsource)
Sheet1.ListBox1.ListFillRange = "Sheet2!" & r.Address
End Sub
regards
FSt1
:
Scenerio:
I have a workbook with 2 worksheets: Main and Filenmames. On the Main
worksheet I have 2 Command Buttons and 1 ComboBox. Behind one command button
("Refresh Parts List and Create Hyperlinks") is code to search a directory of
jpg files and fill column A in the Filenames worksheet with those filenames.
Then in column B of the same Filenames worksheet, hyperlinks are created for
each of the filenames. So, basically, this button just refreshes the
filenames list and hyperlinks.
The second command button ("List Parts") displays a form with a ListBox of
the part numbers. However, I can populate the ListBox if I use the RowSource
Properies, but I have to manually type in "Filenames!B1:B2588". I dont want
to do it this way because the rows in the Filenames worksheet may be
different as new part jpg files are added.
So, my question is: How do I use VBA to define the dynamic variable
RowSource property?
Thank you for your help.