Vba 运行时错误';13';从数组创建字典时类型不匹配
您好,我正在尝试取消打印我的excel工作表(将水平方向转换为垂直方向),我一直得到一个: 运行时错误“13”类型不匹配 在我的CreateDictionaryFromRowArrays函数中。调试器显示错误如下:Vba 运行时错误';13';从数组创建字典时类型不匹配,vba,excel,transpose,type-mismatch,Vba,Excel,Transpose,Type Mismatch,您好,我正在尝试取消打印我的excel工作表(将水平方向转换为垂直方向),我一直得到一个: 运行时错误“13”类型不匹配 在我的CreateDictionaryFromRowArrays函数中。调试器显示错误如下: 对于Idx=1到UBound(键): 有很多方法可以解决这个问题。但是,在您的情况下,它会抛出一个错误,因为Keys为空,并且您正试图使用Keys(1,Idx)访问它。您需要某种类型的防御性编程,在调用函数之前检查参数是否为空 作为一个快速解决方案,向您显示错误的确切位置,将以下内容
对于Idx=1到UBound(键)
:
有很多方法可以解决这个问题。但是,在您的情况下,它会抛出一个错误,因为
Keys
为空,并且您正试图使用Keys(1,Idx)
访问它。您需要某种类型的防御性编程,在调用函数之前检查参数是否为空
作为一个快速解决方案,向您显示错误的确切位置,将以下内容替换为函数:
Public Function CreateDictionaryFromRowArrays(Keys As Variant, _
Items As Variant) As Scripting.Dictionary
Dim Idx As Variant
Dim dic As Scripting.Dictionary
Set dic = New Scripting.Dictionary
If IsEmpty(Keys) Then MsgBox "KEYS ARE EMPTY?"
If IsEmpty(Items) Then MsgBox "ITEMS ARE EMPTY?"
Dim Results As Variant
For Idx = 1 To UBound(Keys)
dic.Add Key:=Keys(1, Idx), Item:=Items(1, Idx)
Next Idx
Set CreateDictionaryFromRowArrays = dic
End Function
解决这个问题的一个可能不那么“丑陋”的方法是:
If Not IsEmpty(varDetails) And Not IsEmpty(varDetailNames) Then
Set dicDetails = CreateDictionaryFromRowArrays(varDetailNames, varDetails)
End If
If Not IsEmpty(varBrokersNames) And Not IsEmpty(varValues) Then
Set dicValues = CreateDictionaryFromRowArrays(varBrokersNames, varValues)
End If
运行此操作时,键和项不是空的。
lCol
的值是多少?'lCol'的值是4SovarValues
是一个单元格。lcol的赋值行中缺少一个点,这可能是问题的原因,也可能不是问题的原因。我将该点添加到赋值行,但仍然会出现类型不匹配错误。您从未将任何内容赋值给varBrokersNames
,如果lcol
为4,varValues
是单个值,而不是数组。
If Not IsEmpty(varDetails) And Not IsEmpty(varDetailNames) Then
Set dicDetails = CreateDictionaryFromRowArrays(varDetailNames, varDetails)
End If
If Not IsEmpty(varBrokersNames) And Not IsEmpty(varValues) Then
Set dicValues = CreateDictionaryFromRowArrays(varBrokersNames, varValues)
End If