我有一个变体数组,如下所示:
“狗” “猫” “马”
我想用
覆盖这些值1.01
2.02
3.03
代码:
sumHolder = loanSums(x)
If sumHolder = "N/A" Then
sumHolder = "0"
End If
loanSums(x) = vbNull
loanSums(x) = sumHolder + trimmedRange(i, trimmedRngCols.paymentAmt)
我试图在字符串上分配双精度,但我得到的是类型不匹配。关于如何做到这一点的任何想法
答案 0 :(得分:6)
你做得不对。
一维变体数组:
Dim a As Long, vals As Variant
vals = Array("dog", "cat", "horse")
For a = LBound(vals) To UBound(vals)
Debug.Print vals(a)
vals(a) = CDbl(a + 1 + (a + 1) / 100)
Next a
For a = LBound(vals) To UBound(vals)
Debug.Print vals(a)
Next a
立即窗口的结果:
dog
cat
horse
1.01
2.02
3.03
二维变体数组:
dim a as long, vals as variant, tmp as variant
vals = range("A2:E4").value2
debug.print lbound(vals, 1) & ":" & ubound(vals, 1) '1:3, 3 'rows' in array
debug.print lbound(vals, 2) & ":" & ubound(vals, 2) '1:5, 5 'columns' in array
for a = lbound(vals, 1) to ubound(vals, 1)
debug.print vals(a, 1) & " - " & vals(a, 3)
tmp = vals(a, 1)
vals(a, 1) = vals(a, 3)
vals(a, 3) = tmp
next a
for a = lbound(vals, 1) to ubound(vals, 1)
debug.print vals(a, 1) & " - " & vals(a, 3)
next a
range("A2").resize(ubound(vals, 1), ubound(vals, 2)) = vals
立即窗口和工作表中的结果:
1:3
1:5
dog - 1.01
cat - 2.02
horse - 3.03
1.01 - dog
2.02 - cat
3.03 - horse
处理 之后
我已经包含了一些前后图像,以便您可以通过左对齐(文本)和右对齐(数字)清楚地看到交换文本的变量编号的单元格值,反之亦然应该不是问题。您的'类型不匹配'必须是值转移以外的其他内容。