使用分割范围值分配数组时出错

时间:2016-11-04 07:09:01

标签: excel vba excel-vba

我正在尝试将字符串从单元格解析为数组以分隔数据并将其放入另一个工作表中。

我一直得到 无法分配到数组 错误,而且我不确定我做错了什么。我确保数据类型是正确的,甚至确保阵列上有足够的空间。

这是我的代码:

Private Sub Enter2_Click()
'Define Variables
Dim MatchRow As Integer
Dim data(7) As String
Dim row As Integer
Dim col As Integer
Dim dataInfo As String

Worksheets("Sheet1").Activate
'Match Name To A Row
MatchRow = WorksheetFunction.Match(RName.Value, Range("A1:A100"), 0)
MsgBox MatchRow
'call report
Cells(MatchRow, 3).Select
data() = Split(ActiveCell.Value, ".", 1) 'This is where the error comes from
MsgBox data(0)
Worksheets("Repoting template").Cells(20, 1).Select

2 个答案:

答案 0 :(得分:4)

改变这个:
data() = Split(ActiveCell.Value, ".", 1)

到此:
data() = Split(ActiveCell.Value, ".")

例:
enter image description here

答案 1 :(得分:3)

如果你有这样的样本数据:

enter image description here

您可以尝试以下代码:

Option Explicit

Sub Test()

    Dim rng As Range
    Dim data() As String

    Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1")
    data = Split(rng.Value, ".", -1, vbBinaryCompare)

    Dim i As Integer
    For i = LBound(data) To UBound(data)
        Debug.Print data(i)
    Next i


End Sub

其中给出了以下输出:

A
B
C
D
E
F
G