我想将值分配给一个整数数组,但我得到了一个编译时错误。
这是我的代码:
Private Sub A()
Dim i As Integer
Dim Width(3) As Integer
Dim TempArray As Variant
TempArray = Array(12, 6, 12, 5)
For i = 0 To 3
Width (i) = CInt(TempArray(i))
Next i
End Sub
错误在这一行:
Width (i) = CInt(TempArray(i))
错误消息是:“编译错误。预期:#”。
我不知道这个说法有什么问题。我使用了保留字吗?我是 VB 新手,但我确实需要帮助。提前致谢。
最佳答案
您的评论使我进行了更多研究。 Width
不仅仅是一个 Excel 保留字,如 Column.Width
.它实际上是一个与写入文本文件有关的 VBA 语句,它期望为输出打开的文件的数量。
这是 Excel 2010 VBA 帮助中的文本:
Width # Statement
Assigns an output line width to a file opened using the Open statement.
Syntax
Width #filenumber, width
The Width # statement syntax has these parts:
Part Description filenumber Required. Any valid file number. width Required. Numeric expression in the range 0–255, inclusive, that indicates how many characters appear on a line before a new line is started. If width equals 0, there is no limit to the length of a line. The default value for width is 0.
Example This example uses the Width # statement to set the output line width for a file.
Dim I
Open "TESTFILE" For Output As #1 ' Open file for output.
VBA.Width 1, 5 ' Set output line width to 5.
For I = 0 To 9 ' Loop 10 times.
Print #1, Chr(48 + I); ' Prints five characters per line.
Next I
Close #1 ' Close file.
关于arrays - 分配给数组时的编译错误 - VBA (Excel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23178140/