尝试将变量更改为数组的运行时错误13

时间:2016-05-18 13:26:03

标签: vba excel-vba excel-2010 excel

这是我编码的第一天,我正在尝试将单元格A1到A20的值添加到数组中,它给了我一个"运行时错误' 13':类型失配"在第4行。

Option Explicit

Sub Dothis()
Dim test() As String
Dim element As Variant
test() = Range("A1:A20").Value
For Each element In test
    If (element > 10) Then element = element + 1
    End
Next element

End Sub

1 个答案:

答案 0 :(得分:0)

你想要这样的东西:

Sub Dothis()
   Dim test() As String
   Dim element As Variant
   Dim element2 As Variant

   Dim i As Integer
   element = ActiveSheet.Range("A1:A20")

   For i = 20 To 10 Step -1
   element(i, 1) = element(i, 1) + 1
   Debug.Print element(i, 1)
   Next i
   element2 = element
   For i = 1 To 20
   Debug.Print element2(i, 1)
   ActiveSheet.Range("B1:B20") = element2
   Next i
End Sub