2017-07-11 149 views
0

这从我以前实际询问的上一个问题继续。我非常渴望找到一种方法来从文本文件中删除尾部空白行,这些文本文件是从我迄今为止不成功的Excel文件生成的。我刚刚发现了下面的代码,当我执行它时,我可以看到它具有我想要的(我认为)的基础,但我没有修改它的技能,因此忽略了其中的任何数据行只是删除空格。任何人都可以帮我修改这个,以便它可以删除那些烦人的空格吗?使用VBA从文本文件中删除空白行

Sub AltText() 
    Dim File As String 
    Dim VecFile() As String, Aux As String 
    Dim i As Long, j As Long 
    Dim SizeNewFile As Long 

    File = Application.GetOpenFilename 

    'Import file lines to array excluding first 3 lines and 
    'lines starting with "-" 
    Open File For Input As 1 
    i = 0 
    j = 0 
    Do Until EOF(1) 
    j = j + 1 
    Line Input #1, Aux 
    If j > 3 And InStr(1, Aux, "-") <> 1 Then 
     i = i + 1 
     ReDim Preserve VecFile(1 To i) 
     VecFile(i) = Aux 
    End If 
    Loop 
    Close #1 
    SizeNewFile = i 

    'Write array to file 
    Open File For Output As 1 
    For i = 1 To SizeNewFile 
    Print #1, VecFile(i) 
    Next i 
    Close #1 

    MsgBox "File alteration completed!" 

End Sub 
+0

因此,您导入文件,并且只是想删除文本后面的空格?你有没有试过['TRIM()'](https://www.techonthenet.com/excel/formulas/trim.php)? – BruceWayne

+0

这是我一直试图做的,但没有太大的成功,我仍然掌握如何有效地工作tbh – Dyhouse

回答

2

要删除空白行,请尝试以下代码:

Sub AltText() 
    Dim inFile As String 
    Dim outFile As String 
    Dim data As String 

    inFile = Application.GetOpenFilename 
    Open inFile For Input As #1 

    outFile = inFile & ".alt" 
    Open outFile For Output As #2 

    Do Until EOF(1) 
     Line Input #1, data 

     If Trim(data) <> "" Then 
     Print #2, data 
     End If 
    Loop 

    Close #1 
    Close #2 

    Kill inFile 
    Name outFile As inFile 

    MsgBox "File alteration completed!" 
End Sub 
+0

除了“.alt”部分,我替换为“.txt”,因为它保存为无法识别的文件格式。我唯一的问题是,它会在文件名中保存为.txt,是否有一种方法可以执行它以使文件名与打开的文件名保持一致? I.e覆盖原始文件? – Dyhouse

+0

我修改了代码来处理这个要求。基本上,只需重命名文件。 –

+0

非常感谢Brian - 这绝对是完美的。我一直认真地把头发拉出来。再次感谢你! – Dyhouse

0

你需要寻找空格和回车符,所以你看行之后,检查内容:

dim temp as string 

temp = Replace (aux, chr(10), "") 
temp = Replace (temp,chr(13),"") 
temp = Rtrim(Ltrim(temp)) ' remove just blank stuff 

现在检查长度:

if j > 3 and Len(temp) <> 0 then 
    ...... 
    add the lines 

所以你的代码应该看起来像这样:

Sub AltText() 
    Dim File As String 
    Dim VecFile() As String, Aux As String 
    Dim i As Long, j As Long 
    Dim SizeNewFile As Long 

    File = Application.GetOpenFilename 

    'Import file lines to array excluding first 3 lines and 
    'lines starting with "-" 
    Open File For Input As 1 
    i = 0 
    j = 0 
    Do Until EOF(1) 
    j = j + 1 
    Line Input #1, Aux 

    '===== 
    dim temp as string 

    temp = Replace (aux, chr(10), "") 
    temp = Replace (temp,chr(13),"") 
    temp = Rtrim(Ltrim(temp)) ' remove just blank stuff 
    '====== 

    If j > 3 And Len(temp) <> 0 Then 
     i = i + 1 
     ReDim Preserve VecFile(1 To i) 
     VecFile(i) = Aux 
    End If 
    Loop 
    Close #1 
    SizeNewFile = i 

    'Write array to file 
    Open File For Output As 1 
    For i = 1 To SizeNewFile 
    Print #1, VecFile(i) 
    Next i 
    Close #1 

    MsgBox "File alteration completed!" 

End Sub 
+0

感谢您的答案。当我尝试执行此操作时,立即给出编译错误“sub或function not defined”,并突出显示“char”。你如何定义这个? – Dyhouse

+0

我的错误,使用CHR而不是字符 - 我总是这样做... – iDebug