我已經寫了一個子例程,它將Excel工作表中的數據轉換爲文本文件並保存,但這會在我的結尾留下很多空行文本文件。由於每次運行該選項卡時可以採用不同數量的數據,因此我認爲這是導致此問題的原因,但我無法看到我需要處理的數據有多少可以改變。有沒有辦法,使用VBA刪除文本文件末尾的空行(空白)或更好的方法,所以它不會首先創建空行?我已經做了一些搜索,並且使用VBA找不到有關此主題的更多信息。請幫忙 ?!刪除文本文件末尾的空行
'Selects appropriate worksheet - Non-MyPayFINAL
Sheets("Non-MyPay FINAL").Select
'Selects all data in column A and copies to clipboard
Range("A1", Range("A1").End(xlDown)).Select
Selection.Copy
'Add a new workbook
Workbooks.Add
'Paste selected values from previous sheet
Selection.PasteSpecial Paste:=xlPasteValues
'Build SaveAs file name (for CSV file)
MySaveFile = Format(Now(), "DDMMYYYY") & "NonMyPayFINAL" & ".CSV"
'Save template file as...(for CSV file)
ActiveWorkbook.SaveAs ("S:\MERIT OUTPUTS FOLDER\MSI Recruitment
Limited\" & MySaveFile), FileFormat:=xlCSV
'Build SaveAs file name (for Txt file)
MySaveFile = Format(Now(), "DDMMYYYY") & "NonMyPayFINAL" & ".Txt"
'Save template file as...(for Txt file)
ActiveWorkbook.SaveAs ("S:\MERIT OUTPUTS FOLDER\MSI Recruitment
Limited\" & MySaveFile), FileFormat:=xlTextWindows
我已經更新了代碼段,它複製並創建了我認爲是問題所在的文本文件。任何幫助,這將不勝感激。
我發現下面的代碼,通過允許我將文本添加到txt文件的末尾,可以同樣用於刪除文本以及如果這看起來很簡單,但我沒有相當有這個懸而未決。
Sub TextFile_Create()
'PURPOSE: Add More Text To The End Of A Text File
Dim TextFile As Integer
Dim FilePath As String
'What is the file path and name for the new text file?
FilePath = "S:\MERIT OUTPUTS FOLDER\MSI Recruitment
Limited\11072017MyPayFINAL.txt"
'Determine the next file number available for use by the FileOpen
function
TextFile = FreeFile
'Open the text file
Open FilePath For Append As TextFile
'Write some lines of text
Print #TextFile, "Sincerely,"
Print #TextFile, ""
Print #TextFile, "Chris"
'Save & Close Text File
Close TextFile
End Sub
說實話,修改你現有的代碼只複製當前在Excel文件中的行數可能會更好。這不應該添加太多到您的代碼,但將刪除從文本文件 – danl