We help IT Professionals succeed at work.
Get Started

Excel VBA:  How to add items to a multi-select listbox with two columns instead of one?

Last Modified: 2019-12-30
Excel VBA form:  How to add items to a multi-select listbox with two columns instead of one?

Please note the following code:
I get the yellow highlight on line item with Run-time error '13'.  type mismatch.
please note image attachments that show this as well.

All I'm doing is looping through the collection and adding to the two columns - the fullfilename found in variable sFile and the filename only in sFileOnly...

Me.ListBox_FilesFound(lCounter - 1, 0) = sFile

Open in new window

Private Sub FillListBox_FilesFound()
On Error GoTo Err_Proc

  Dim sFileName As String
  Dim sFilePath As String
  Dim lCounterFileTotal As Long
  Dim lCounter As Long
  Dim sFileOnly As String
  Dim colFiles As New Collection
  Dim vFile As Variant
  Dim sFile As String
  'setting up listbox:
  Me.ListBox_FilesFound.ColumnCount = 2
  Me.ListBox_FilesFound.ColumnWidths = "0;100"
  sFileName = GetCurrentFileName

  'determine path
  sFilePath = FilePathOnly_feo(sFileName)
  txtFilePath = sFilePath
  RecursiveDir colFiles, sFilePath, "*.xls*", True
  lCounterFileTotal = colFiles.Count
  lCounter = 0
  'loops through all files found in RecursiveDir
  'but only through filenames that have states.
  For Each vFile In colFiles
    sFile = vFile
    lCounter = lCounter + 1
    sFileOnly = FileNameOnly_feo(sFile)
    Me.ListBox_FilesFound(lCounter - 1, 0) = sFile
    Me.ListBox_FilesFound(lCounter - 1, 0) = sFileOnly
    If IsNameOfUSStates(sFileOnly) = True Then
      ListBox_FilesFound(lCounter - 1, 0).Selected = True
    End If
  Next vFile
  'lCounter = 0
  Exit Sub
  Call LogError_feo(Err, Err.Description, "fAdjustFiles @ FillListBox_FilesFound")
  Resume Exit_Proc
End Sub

Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE