如何修复VBA运行时错误70权限被拒绝

时间:2018-09-05 22:15:56

标签: excel permissions

我正在尝试基于另一个工作表上一列数据的值填充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

2 个答案:

答案 0 :(得分:0)

组合框是在Excel中的设计模式下创建的。将其“ ListFillRange”属性设置为另一张工作表上的单元格值范围。一旦删除了它的ListFillRange属性并从组合框的该属性中删除了所有内容,我再也看不到此错误。代码可以编译并且似乎可以正常工作。

答案 1 :(得分:0)

关于此VB错误,StackOverflow上有多个答案。每个答案或情况在现实中都是唯一的-尽管每个现有答案都指出了不同的潜在根本原因(文件权限,文件夹权限,名称重用,范围等)。

我建议双击表示功能/代码的一侧以缩小根源,以标记断点(看起来像一个红点)(或者,您可以右键单击代码行) -选择Toggle,然后选择Breakpoint)。

接下来,运行您的代码,它将在您的断点处停止。然后,您可以单步执行/移入/移出代码,并从根本上找到负责引发错误代码的代码行。 (进入F8,进入Shift+F8(进入Debug顶部菜单以查看更多选项))

确定了负责的代码行后,您就可以开始进一步查找了。

在我的情况下,我使用的是受保护的变量名称“ Date”(查找variable names)。将其重命名为其他名称后,问题已解决。