2017-07-11 173 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