Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
导出时,Excel VBA会在文本文件末尾添加额外的空行_Vba_Text_Excel - Fatal编程技术网

导出时,Excel VBA会在文本文件末尾添加额外的空行

导出时,Excel VBA会在文本文件末尾添加额外的空行,vba,text,excel,Vba,Text,Excel,我有一个Excel VBA宏,可以输出到文本文件。文本文件的底部总是有一个空行,我很难摆脱它。任何有用的建议都将不胜感激!谢谢 Sub testExport() Dim fPath As String, exportTxt As String fPath = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\Sample_" & Format(Now(), "HHNNSS") & ".txt"

我有一个Excel VBA宏,可以输出到文本文件。文本文件的底部总是有一个空行,我很难摆脱它。任何有用的建议都将不胜感激!谢谢

Sub testExport()

  Dim fPath As String, exportTxt As String
  fPath = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\Sample_" & Format(Now(), "HHNNSS") & ".txt"

  exportTxt = "Project: Sample Output" & vbCrLf
  exportTxt = exportTxt & "Model Version: 1 "


  Open fPath For Append As #1    'write the new file
  Print #1, exportTxt
  Close #1

Print#
语句的帮助中,以及在数据输出后指定
charpos
的方法,结束Sub

charpos指定下一个字符的插入点。使用 分号来定位插入 紧跟在最后一点之后 显示字符。使用制表符(n)来 将插入点定位到 绝对列数。使用制表符 没有用于定位插入的参数 点在下一节的开头 打印区。如果省略charpos,则 下一个字符打印在下一个字符上 线路

请尝试以下操作:

Print #1, exportTxt;

它在一行中打印所有内容。根本没有换行。

我所做的工作是,在创建txt文件之后, 用我想要复制的数据计算行数,然后循环使用Print#1,exportTxt创建一个新文件;并打印#1,“,最后一行除外

Open sOrgFile For Input As #1
Do While Not EOF(1)
iCounter = iCounter + 1
Line Input #1, sData
If sData = "" Then Exit Do
Loop
Close #1

Open sOrgFile For Input As #1
Open sDestFile For Output As #2
Do While Not EOF(1)
iCounter2 = iCounter2 + 1
Line Input #1, sData
    If sData = "" Then Exit Do
Print #2, sData;
If iCounter2 <> iCounter Then Print #2, ""
sData = ""
Loop
打开文件以作为#1输入
不执行时执行EOF(1)
i计数器=i计数器+1
行输入#1,sData
如果sData=“”,则退出Do
环
关闭#1
打开文件以作为#1输入
打开sDestFile,输出为#2
不执行时执行EOF(1)
iCounter2=iCounter2+1
行输入#1,sData
如果sData=“”,则退出Do
打印#2,sData;
如果i计数器2 i计数器,则打印#2“
sData=“”
环

如果我只想为最后一个空行执行该操作,该怎么办?
Open sOrgFile For Input As #1
Do While Not EOF(1)
iCounter = iCounter + 1
Line Input #1, sData
If sData = "" Then Exit Do
Loop
Close #1

Open sOrgFile For Input As #1
Open sDestFile For Output As #2
Do While Not EOF(1)
iCounter2 = iCounter2 + 1
Line Input #1, sData
    If sData = "" Then Exit Do
Print #2, sData;
If iCounter2 <> iCounter Then Print #2, ""
sData = ""
Loop