首页 > 解决方案 > Excel VBA not writing to next text file line

问题描述

I have the following macro that is should write new data onto a new line in a text/csv file, however it overwriting the existing text in the file, I thought that write wrote to a new line as opposed to print. I am not sure what I am doing wrong

Sub picking()

Range("d14").NumberFormat = "mm/dd/yyyy"
Range("d14").Value = Now

Range("e14").NumberFormat = "hh:mm"
Range("e14").Value = Now

Range("e17").Value = "Picking"

Call outputcsv

End Sub



Sub outputcsv()

Dim Fullpath As String
Dim outputstring As String

Fullpath = Worksheets("Maintence").Range("c5")

Open Fullpath For Output As #1
outputstring = Worksheets("CSV").Range("A1")
Write #1, outputstring
Close #1
End Sub

标签: excelvba

解决方案


你只需要改变...

Open Fullpath For Output As #1

...至...

Open Fullpath For Append As #1

请注意,“输出”更改为“附加”。

另请注意,“附加”将创建文件 - 就像输出一样 - 如果它尚不存在。无需预先创建它。


推荐阅读