RowSource property error vba

2019-08-21 17:09发布

I am trying to write a code that converts an input range into an array, then feeds that array into the RowSource for a listbox in my form.

According to MSDN the "RowSource" property for list/combo box should accept arrays, so I'm not sure why Im getting a runtime error here:

 Option Explicit

Sub test()
Dim rng As Range
Dim myarray As Variant


Set rng = Worksheets("Sheet1").Range("List")
myarray = RangeToArray(Range("List"))
UserForm1.ListBox1.RowSource = "myarray"


End Sub
Function RangeToArray(inputRange As Range) As Variant
   Dim inputArray As Variant
   inputArray = inputRange.Value

   'operations on inputArray
   '...'

   RangeToArray = inputArray
End Function

2条回答
成全新的幸福
2楼-- · 2019-08-21 17:19

According to the Excel VBA help:

The RowSource property accepts worksheet ranges from Microsoft Excel.

If you have a MSDN link that says otherwise, please post it.

Anyway, to get the result you want, change

UserForm1.ListBox1.RowSource = "myarray"

to

UserForm1.ListBox1.List = myarray
查看更多
成全新的幸福
3楼-- · 2019-08-21 17:28

try UserForm1.ListBox1.List = myarray

查看更多
登录 后发表回答