两个字符串不会连接 VBA Excel



我正在用VBA写一个小的Excel宏。现在我想连接两个字符串并将它们保存到一个字符串数组中。

我得到了什么:

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`

没有按照应有的方式连接字符串。我做错了什么?我认为&运算符将始终适用于VBA中的字符串。

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

不确定您要完成什么,但是使用Objects而不是StringsFor...Next语句应该可以帮助您完成任务。

Option Explicit
Sub TEST()
    Dim ws As Worksheet, Rng12 As Range, Cell As Range
    Set ws = ThisWorkbook.Worksheets(1)
    Set Rng12 = ws.Range("L12:Z12") 'Adjust your range
    For Each Cell In Rng12
        Debug.Print Cell.Address
    Next Cell
End Sub

最新更新