親愛的,我收到一個帶有電子郵件的 csv 檔案,我想在 Outlook 中使用 vba 代碼洗掉其中的 5 或 6 行,不知何故,我認為將 csv 檔案放入字串中然后做我想做的事情會更容易,然后再次將其另存為 csv 檔案,我已經完成了將其存盤在字串中并將其再次保存為 csv 檔案的程序,但是我不知道如何在文本位于字串內時洗掉行,這就是我的方式將 csv 檔案放在一個字串中
Dim iTxtFile As Integer
Dim strFile As String
Dim strFileText As String
strFile = "C:\Users\Hussein.azad\Desktop\4G.csv"
iTxtFile = FreeFile
Open strFile For Input As FreeFile
strFileText = Input(LOF(iTxtFile), iTxtFile)
Close iTxtFile
strFileText = Replace(strFileText, "NIL", "0")
uj5u.com熱心網友回復:
Sub fs_DeleteRowsFromTextFile()
Const FOR_READING = 1
Const FOR_WRITING = 2
' Text file full path
Dim strFileName As String: strFileName = "C:\deletefirstNlines.txt"
' Number of lines to delete
Dim LinesToDelete As Long: LinesToDelete = 6
Dim oFS As Object: Set oFS = CreateObject("Scripting.FileSystemObject")
' Set Text Stream for reading
Dim oTS As Object: Set oTS = oFS.OpenTextFile(strFileName, FOR_READING)
' Copy txt stream contens to variable
Dim strContents As String: strContents = oTS.ReadAll
' Close text stream
oTS.Close
' Split file into array
Dim arrLines() As String: arrLines = Split(strContents, vbNewLine)
' Set Text Stream for writing
Set oTS = oFS.OpenTextFile(strFileName, FOR_WRITING)
' Write array back to file exluding 'LinesToDelete'
Dim i As Long
For i = 0 To UBound(arrLines)
If i > (LinesToDelete - 1) Then
oTS.WriteLine arrLines(i)
End If
Next
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/353786.html
標籤:vba
上一篇:更改現有文本框形狀的字體大小
