做到这一点的一种方法是知道文件中有多少行,所以先通读一遍,然后数一数。然后,当您编写它们时,执行
RemoveTrailing
如果输出的行是第一行、第二行、倒数第二行或最后一行。。
尝试以下操作:
Sub ExportAsCSV()
Dim MyFileName As String
Dim CurrentWB As Workbook, TempWB As Workbook
Set CurrentWB = ActiveWorkbook
ActiveWorkbook.ActiveSheet.UsedRange.Copy
Set TempWB = Application.Workbooks.Add(1)
With TempWB.Sheets(1).Range("A1")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
End With
MyFileName = CurrentWB.Path & "\" & Left(CurrentWB.Name, Len(CurrentWB.Name) - 5) & ".csv"
Application.DisplayAlerts = False
TempWB.SaveAs Filename:=MyFileName, FileFormat:=xlCSV, CreateBackup:=False, Local:=True
TempWB.Close SaveChanges:=False
Application.DisplayAlerts = True
Dim sFile2 As String
Dim sLine As String
sFile2 = Replace(MyFileName, ".csv", "SANS_VIDE.csv")
Dim rowcount As Long, thisrow As Long
Open MyFileName For Input As #1
rowcount = 0
Do Until EOF(1)
Line Input #1, sLine
rowcount = rowcount + 1
Loop
Close #1
Open MyFileName For Input As #1
Open sFile2 For Output As #2
thisrow = 0
Do Until EOF(1)
thisrow = thisrow + 1
Line Input #1, sLine
If thisrow <= 2 Or thisrow >= rowcount - 1 Then
Print #2, RemoveTrailing(sLine)
Else
Print #2, sLine
End If
Loop
Close #1
Close #2
End Sub