两个string不会连接VBA Excel
我正在用VBA写一点Excel-Macro。 现在我想连接两个string并将其保存到一个string数组中。
我得到了什么:
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。 我做错了什么? 我认为&
-Operator会一直为VBA中的Strings工作。
正如我在评论中所说的,你可以用完全不同的方式来解决这个问题。
不知道你想要完成什么,但是使用Objects
而不是Strings
的For...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