2017-10-09 17 views
1

私はVBAで少しのExcelマクロを書いています。今私は、2つの文字列を連結し、文字列配列に保存したいと思います。2つの文字列は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` 

がStringにそれが必要な方法を連結されていない私が得たもの

。私は何を間違えたのですか?私は& - オペレーターは常にVBAの文字列のために働くと思った。

+0

ここでどのように 'colIndexArray'が定義されていますか? –

+0

関連:https://stackoverflow.com/questions/1727699/how-to-concatenate-strings-in-vba – jsheeran

+0

です。仕事には他に何かがあるはずです。 – Rory

答えて

2

私のコメントで述べたように、これはまったく別の方法でこれを行うことができます。

Stringsではなく、Objectsを使用して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 
+0

'Do While ... Wend'ではなく、' For ... Next'を使って新しいメソッドを表示するように更新されました。 –

関連する問題