我想与SAP进行交易,我使用cvs文件作为导出文件,但最后我想删除此文件的内容而不删除它,只是删除内容。
Sub OpenCSVFile()
'
' Load the CSV extract
'
'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & fpath & "\" & ffilename, Destination:=Range("$A$1"))
.Name = "text"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "|"
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
With ActiveSheet
.Columns(1).EntireColumn.Delete 'delete first column
.Rows("1:7").EntireRow.Delete 'delete first 7 rows
End With
End Sub
答案 0 :(得分:2)
Sub ClearContents()
Open "C:\Users\Username\Desktop\test1.csv" For Output As #1: Close #1
MsgBox "Clear complete"
End Sub
只需将文件路径更改为csv文件所在的位置即可。
答案 1 :(得分:1)
如果您要清除csv文件的内容,请尝试一下此宏
Sub clear()
Workbooks.Open Filename:="C:\Users\p100789\Documents\SAP\SAP GUI\test.csv"
Cells.ClearContents
Activeworkbook.Close SaveChanges:=True
End Sub
注意:,您必须将文件路径替换为您的文件路径
如果.CSV文件位于同一文件夹中,则可以使用以下命令行自动选择目录:
filepath = ActiveWorkbook.Path & "\"
所以您的代码将是:
Sub clear()
Dim filepath As String
Dim FileName As String
filepath = ActiveWorkbook.Path & "\"
FileName = "Book2.csv"
Workbooks.Open Filename:=filepath & Filename
Cells.ClearContents
Activeworkbook.Close SaveChanges:=True
End Sub
希望此帮助=)