分配长字符串文字
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.