arrays - 分配给数组时的编译错误 - VBA (Excel)

标签 arrays excel compilation assign vba

我想将值分配给一个整数数组,但我得到了一个编译时错误。

这是我的代码:

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/

相关文章:

javascript - Javascript 中的空数组值

c# - C#编译器的泛型、继承和失败的方法解析

c++ - 如何在 Visual Studio C++ 2005 中处理所有#include 指令?

c++ - 如何在 C++ IDE 代码块或开发 C++ 中更改编译器?

c - 按字母顺序对单词数组进行排序

php - 自然排序关联数组?

excel - 不使用循环操作数组

mysql - 如何创建一个数据库来接收来自其他数据库的数据、手动输入和数据导出以创建 Web 仪表板?

javascript - 循环遍历数组并将文本应用于 n :th element

excel - 我可以将 =Offset() 的范围结果存储在另一个单元格中吗?