You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

向ComboBox添加数组元素时出现运行时错误‘70’:权限被拒绝

运行时错误‘70’:权限被拒绝(ComboBox添加项失败)

我尝试将数组中的项添加到ComboBox中。现有materialArray数组,通过in_array()函数筛选其中的唯一项并存入新数组uniqueMaterial,但在执行代码行ActiveSheet.ComboBox2.AddItem uniqueMaterial(i)时,触发以下错误:

"运行时错误‘70’:权限被拒绝"

以下是使用的代码及配套函数:

主过程代码

Sub ComboBox2_DropButtonClick()

Dim lastRowMaterial As Long
Dim uniqueMaterial() As Variant
Dim materialArray() As Variant

lastRowMaterial = Sheet1.Cells(Rows.Count, "H").End(xlUp).Row         '获取H列(物料)的行数
    materialArray = Sheet1.Range("Data!H4:H" & lastRowMaterial)       '将整个物料列表存入数组
    ReDim uniqueMaterial(0)
    
    If Is_Var_Array_Empty(uniqueMaterial) Then
        uniqueMaterial(0) = materialArray(1, 1)
    End If
    
    For i = LBound(materialArray) To UBound(materialArray)
        If Not in_array(uniqueMaterial, materialArray(i, 1)) Then
            ReDim Preserve uniqueMaterial(UBound(uniqueMaterial) + 1)
            uniqueMaterial(UBound(uniqueMaterial)) = materialArray(i, 1)
        End If
    Next
    
    For i = LBound(uniqueMaterial) To UBound(uniqueMaterial)
        ActiveSheet.ComboBox2.AddItem uniqueMaterial(i)
                        '将唯一物料复制到下拉列表
    Next
                                                
End Sub

Is_Var_Array_Empty函数(检查数组是否为空)

Function Is_Var_Array_Empty(arr_var As Variant)     '判断数组是否为空的函数
 
Dim p As Integer
 
On Error Resume Next
    p = UBound(arr_var, 1)
If Err.Number = 0 Then
    Is_Var_Array_Empty = False
Else
    Is_Var_Array_Empty = True
End If
 
End Function

in_array函数(检查值是否在数组中)

Function in_array(my_array, my_value)
    
    in_array = False
    
    For i = LBound(my_array) To UBound(my_array)
        If my_array(i) = my_value Then '找到匹配值
            in_array = True
            Exit For
        End If
    Next
End Function

内容的提问来源于stack exchange,提问作者Hampus T

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 09:24:25