我正在尝试用动态范围的值填充组合框(拖到我的工作表上)。
Normaly 'ThisWorkbook.Names("NAME_OF_RANGE").RefersToRange' 有效,但它在我的动态范围内失败!
我尝试了不同的解决方案(参见下面的代码),但随后工作表名称从引用中被删除(因此填充了错误的数据)
Public Sub FillCombobox(cboComboBox As ComboBox, sRange As String)
'Remember current selected item
Dim sSelectedItem As String
sSelectedItem = cboComboBox.Text
'Empty the combobox
cboComboBox.Clear
cboComboBox.AddItem "---pick one---"
'Get the data from the dynamic range
Dim oListFillRange As Range
'This line will throw an error:
Set oListFillRange = ThisWorkbook.Names(sRange).RefersToRange 'Does not work with a dynamic range!!!
''Set oListFillRange = Range(Application.Evaluate(ThisWorkbook.Names(sRange).RefersTo).Address) 'Works with dynamic ranges on the SAME SHEET (as the sheetname is stripped out!)
'Fill combobox
Dim oRange As Range
For Each oRange In oListFillRange
cboComboBox.AddItem oRange.Value
Next oRange
'Set previous selected item
Dim i As Integer
For i = 0 To cboComboBox.ListCount - 1
If cboComboBox.List(i) = sSelectedItem Then
cboComboBox.ListIndex = i
Exit for
End If
Next i
If cboComboBox.ListIndex = -1 Then cboComboBox.ListIndex = 0
End Sub
那么如何让 'ThisWorkbook.Names("NAME_OF_RANGE").RefersToRange' 使用动态范围?