声明并使用Array - 获取类型不匹配

时间:2014-08-14 17:03:53

标签: excel-vba vba excel

如果前一列为空,我正在尝试构建一个For循环来清除单元格的内容。我构建一个数组,因为我必须分开列来检查。这是我的代码:

    With calcCalculations

Dim rng As Range
Dim xRange(1 To 2) As String

xRange(1) = "CA"
xRange(2) = "CD"

    **For Each rng In .Columns(xRange).SpecialCells(xlBlanks)**
        rng.Offset(, 1).ClearContents

                Next

    End With

然而,当我运行我的代码时,我在突出显示的行中出现Type MisMatch错误 任何想法???

由于

1 个答案:

答案 0 :(得分:1)

你需要索引数组:

Dim rng As Range
Dim xRange(1 To 2) As String

xRange(1) = "CA"
xRange(2) = "CD"
For i = 1 To 2
    For Each rng In .Columns(xRange(i)).Cells.SpecialCells(xlBlanks)
        rng.Offset(, 1).ClearContents
    Next
Next i