分配長字串文字
VBA 編輯器每行只允許 1023 個字元,但通常只有前 100-150 個字元可見而不滾動。如果你需要分配長字串文字,但希望保持程式碼可讀,則需要使用行繼續和連線來分配字串。
Debug.Print "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " & _
"Integer hendrerit maximus arcu, ut elementum odio varius " & _
"nec. Integer ipsum enim, iaculis et egestas ac, condiment" & _
"um ut tellus."
'Output:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer hendrerit maximus arcu, ut elementum odio varius nec. Integer ipsum enim, iaculis et egestas ac, condimentum ut tellus.
VBA 將允許你使用有限數量的行繼續(實際數字因連續塊中每行的長度而異),因此如果你有很長的字串,則需要分配並重新分配連線。
Dim loremIpsum As String
'Assign the first part of the string
loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " & _
"Integer hendrerit maximus arcu, ut elementum odio varius "
'Re-assign with the previous value AND the next section of the string
loremIpsum = loremIpsum & _
"nec. Integer ipsum enim, iaculis et egestas ac, condiment" & _
"um ut tellus."
Debug.Print loremIpsum
'Output:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer hendrerit maximus arcu, ut elementum odio varius nec. Integer ipsum enim, iaculis et egestas ac, condimentum ut tellus.