title: How to populate userform listbox with a filtered range from a worksheet
body: <p>I have a UserForm listbox where I want to populate a filtered range (only visible cells) from an excel Worksheet. Problem comes whith this sentence: <code>userForm.listBox.RowSource = dataRng.Address</code> where dataRng is the Range of visible cells (<code>Set dataRng = sht.Range(Cells(startRow, 1), Cells(lastRow, 4)).SpecialCells(xlCellTypeVisible)</code>).</p>
  
  <p>I tried to get the visible range with a dynamic array too (<code>MyArray(i, j) = dataRng.Cells(i, j).Value</code>) and then populate listbox with it, but without succes (here comes header issues but seems the most efficient and fastest solution). </p>
  
  <p>I found <a href="https://stackoverflow.com/questions/15334838/excel-filter-in-sheet-and-populate-filtered-range-in-userform-listbox">this unanswered question</a> from some years ago but I'm looking for a better solution.</p>
  
  <pre><code> Private Sub listBox_Change()
      Dim startRow,lastRow As Integer
      Dim sht As Worksheet
     'Dim MyArray As Variant 'variant, receives one based 2-dim data field array
  
          Set sht = Worksheets("SheetName")
          Call filterData(sht) 'filter data in SheetName
          startRow = sht.Cells(Rows.Count, 1).End(xlDown).Row 'get initial row of filtered range
          lastRow = sht.Cells(Rows.Count, 1).End(xlUp).Row 'get last row of filtered range
          Set dataRng = sht.Range(Cells(startRow, 1), Cells(lastRow, 4)).SpecialCells(xlCellTypeVisible) 'get range of visible cells in SheetName
  
          With userForm.listBox
  
              .ColumnCount = 4
              .ColumnWidths = "90;90;0;90"
              .RowSource = dataRng.Address
  
      '    For i = startRow To lastRow
      '        For j = 1 To 3
      '            MyArray(i, j) = dataRng.Cells(i, j).Value
      '        Next j
      '    Next i
  
          End With
  
      End Sub
  </code></pre>
  
  <p>Run-time error 380: Could not set the RowSource property. Invalid property value.</p>
  
  <p><a href="https://i.stack.imgur.com/caoET.png" rel="nofollow noreferrer">Error message</a></p>