两个字符串不会连接VBA Excel

时间:2017-10-09 14:41:45

标签: excel vba excel-vba

我正在用VBA编写一个Excel-Macro。现在我想连接两个字符串并将它们保存到String-Array中。

我得到了什么:

Dim rowNumberString As String
Dim colIndexString As String
Dim headerArray(1 To colIndexArrayRange) As String

colIndexNumber = 14
colCount = 5
rowNumberString = "12"
addAnotherColumnToArray = True

' Fill the column array with all the month's entries
While addAnotherColumnToArray
    colCount = colCount + 1
    colIndexNumber = colIndexNumber + 1

    If colIndexArray(colCount) = "" Then
        colIndexString = Split(Cells(1, colIndexNumber).Address(True, False), "$")(0)
        colIndexArray(colCount) = colIndexString & rowNumberString
    End If

    Debug.Print colCount & "=" & colIndexArray(colCount)

    If (colIndexNumber > 61) Then
        addAnotherColumnToArray = False
    End If
Wend

输出:

6=O
7=P
8=Q
9=R
10=S
11=T
12=U
' ....

所以看来这一行:

` colIndexArray(colCount) = colIndexString & rowNumberString`

不是以它应该的方式连接String。我做错了什么?我认为& - 运算符总是适用于VBA中的字符串。

1 个答案:

答案 0 :(得分:2)

正如我在评论中所说,你可能会以完全不同的方式解决这个问题。

不确定您要完成的工作,但使用For...Next而不是Objects的{​​{1}}语句可以帮助您完成任务。

Strings