繁体   English   中英

为什么在遍历(有时为空)变体时会出现(运行时错误“13”:)类型不匹配?

[英]Why am I getting (run-time error '13': ) type mismatch when looping across a (sometimes-empty) variant?

我正在使用Private Sub Worksheet_Change(ByVal Target As Excel.Range)事件(第一次)在单个工作表中构建动态验证列表。 单元格 A2 和 B2 包含验证下拉列表; 单元格 B2 取决于单元格 A2; 并且单元格 A2 取决于 A 列中超出 A5 的其余值

[在此列表中,B2 中的最终选择将产生与其相关的其他值:DB-like ]

这是工作表更改后子程序的工作方式:

VARIABLE DECLARATIONS* variant, range, string

设置三个范围对象 A、B 和 C,其中 B 和 C 使用 A(范围中最后使用的单元格)

检查目标的位置

  1. 如果目标在第一列(A 列)中,

1a) 查看目标是否在第二行 (A2)...如果是,则清除 B 范围内的内容,调用 function1 返回过滤值数组及其在活动工作表上的单元格位置 >> 分配将数组返回到在后续调用中使用的变体变量 1

  • 调用函数1
  • 变量 1 = 函数 1
  • 调用函数2( Target ,变量1)
  1. 如果目标在第二列 (B) 中,

2a)查看目标是否在第二行(B2)...如果是,则清除C范围内的内容,调用函数以根据B2中的内容填充C范围内的单元格; 如果目标不在 B2 中,则不应发生其他任何事情

  1. 如果目标在其他任何地方

...没发生什么事


问题发生在 call function2; 当我观察变量/在值更改时添加中断时,函数 1 和变量 1 似乎正确接收值,但是当调用函数 2 时,它会抛出运行时错误并告诉我变量 1 为空...

我不知道在初始执行期间对范围所做的更改是否算作对工作表的后续更改,或者它们是否都算作相同的更改

我已经成功使 A2 和 B2 中的下拉列表起作用,但是 B2 中的更改并没有促进最终更改,因此重新配置了代码......重新配置的代码产生了奇怪的错误追踪

我确实尝试停用/激活 enableEvents,(对我来说也是新的)但它没有改变结果......下面是来自 function2 (arg1, arg2) 的代码......如果在它之前一切正常,我不明白为什么它返回错误

Call StateFinder                                           'calls the function that builds address 
SearchAll50 = StateFinder                                  'sets the new searchable array to it's value
Call ListBuilder(Target, SearchAll50)                      'builds a list of values in the target cell
                                
                                
'a variable set that stores the Target's address
'-----------------------------------------------
StateGPS = GetPlaceGPS(Target.Value, SearchAll50)           'the Target address will be set to variable

GetPlaceGPS 抛出错误

Function GetPlaceGPS(ByVal SelectedPlace As String, ByVal MadeList As Variant) As String
'the returned address will then be used to set the range used to search cities

    Dim i As Long     
    For i = LBound(MadeList) To UBound(MadeList)
    
        If MadeList(i)(0) = SelectedPlace Then
            'compares the first value of the ith row to the selected state
            'once the state is found GetStateGPS is set to the sheet address
                
             GetPlaceGPS = MadeList(i)(1)
            
            'exit the function once the value is properly set
             Exit For
        End If        
    Next i
    
End Function

必须使用字符类型参数并且必须声明为变量。

Dim myString As String
myString = target.Value
StateGPS = GetPlaceGPS(myString, SearchAll50) '<~~ Character type arguments must be used and must be declared as variables.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM