我试图在Excel中填充一个组合框,基于另一个工作表上的一列数据的值。我收到运行时70权限被拒绝错误。任何帮助,将不胜感激。 (.AddItem - 抛出错误)
Sub Fill_Combo()
'Turn on the enabling of events in Excel, if it is not already on.
Application.EnableEvents = True
' Create a new worksheet object to reference
Dim ws As Worksheet
Dim rg As Range
Dim cmb As ComboBox
Dim strVal As String
'Set the new worksheet object to the worksheet you want to reference.
Set ws = Workbooks("Test 6043 v3.xlsm").Worksheets("Ending Balances")
Set rg = ws.Range("G1")
Set cmb = Workbooks("Test 6043 v3.xlsm").Worksheets("Reconciliation").cmbBox2
'Reference the combobox and start adding items to it.
'With Workbooks("Test 6043 v3.xlsm").Worksheets("Reconciliation").cmbBox2
'First, clear the combobox if there is anything in it.
'.Clear
'Check to see whether G1 on the Ending Balances sheet is empty.
If IsEmpty(rg.Value) Then
'If it is then...
Do
'Move down by one row
Set rg = rg.Offset(1, 0)
'Keep doing this until a non-empty row is found
Loop Until (Not (IsEmpty(rg.Value)))
'Then, once one range (cell) is found that is non-empty
Do
strVal = rg.Value
With cmb
'Add its value to the combo box.
**.AddItem strVal**
'Keep looping and add these values in Column G until the first non-empty row is found,again.
End With
'Move down by another row
Set rg = rg.Offset(1, 0)
Loop Until (IsEmpty(rg.Value))
Else
组合框是在Excel中以设计模式创建的。其“ListFillRange”属性设置为另一个工作表上的单元格值范围。一旦我删除了它的ListFillRange属性并从组合框的该属性中删除了所有内容,我就不再看到此错误了。代码编译并且似乎有效。