使用 VBA 写入 TXT 文件 - 文件被中断

Write TXT file with VBA - File is interrupted

我有一个 VBA 宏,它在 运行 安装 X13 ARIMA SEATS 之前以 "datevalue" 格式(即年标签月标签数据)写入一个 txt 文件。

一直有效,但最近只有几次有效。当我查看它在季节性调整之前生成的 .txt 文件时,它在一行中间被打断了。从 1998 年到 2018 年 7 月,它像往常一样写入文件,但它停在小数点的中间。

这是编写 txt 日期值文件的代码部分。 Ano 和 mes(年和月)是代码开头的输入。

Private Sub copyData(ByVal mes As Integer, ByVal ano As Integer, rng As Range)
    Dim sFileText As String
    Dim iFileNo As Integer
    Dim i, col As Double

    i = rng.Row
    col = rng.Column
    ficar = True
    iFileNo = FreeFile

    Open "P:\Macro\X12\Input\serie1.txt" For Output As #iFileNo

    'Writes the file with the time series
    While Cells(i, col) <> ""
        Print #iFileNo, ano & Chr(9) & mes & Chr(9) & Cells(i, col)
        If (mes + 1) Mod 12 > 0 Then mes = (mes + 1) Mod 12 Else mes = 12
        If mes = 1 Then ano = ano + 1
        i = i + 1
    Wend

    Close #iFileNo

End Sub

这是 .txt 文件的头部

1998    1   4641.272855
1998    2   3943.235604
1998    3   5167.087047
1998    4   4629.068494
1998    5   4736.139222
1998    6   4703.891762
1998    7   5394.787069
1998    8   4155.992635
1998    9   5741.168184
1998    10  5460.08048

这是尾巴,它停止工作的地方。

2018    4   13790.364479
2018    5   13259.844355
2018    6   14320.106493
2018    7   16101.08

我的猜测是现在的文件太长了。

更新!!
我在 Wend 之前添加了一个 MsgBox(如下所示),因此代码会 运行 变慢(我在 运行s 时一直按回车键)并且它起作用了。所以我认为它打破了 file-writing 因为它花费的时间太长了。

MsgBox ano & Chr(9) & mes & Chr(9) & Cells(i, col)

跟进(如果有人遇到过这个问题):

我认为该文件对我的 excel 来说太长了,而且有些不知所措。解决方案是限制 运行 代码前的小数位数。所以我在 excel 文件上放了一个 =ROUND(xxx, 2) !

希望对您有所帮助!