在Excel 2013中执行macros后删除列

我写了macros从csv文件导入三列到excel文件有4列。 1列是根据3来自csv文件制定的。 所以在运行macros之前,有3个空白列(甚至没有列名)和第4列有缺省值的excel文件。 现在,当我运行macros,3列正在importfrm csv但第四列正在被删除。我不知道为什么会发生这种情况。 我已经使用loggingmacrosfunction来创buildmacros。 以下是我的macros代码:

Private Sub Workbook_Open() Sheet11.Cells.ClearContents With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;D:\Sample SSRS\power View\AlertHistory.csv", Destination:=Range("$A$1") _ ) .Name = "AlertHistory" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 437 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1, 1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With End Sub 

请帮忙。

您正在清理整个工作表,

 Sheet11.Cells.ClearContents 

第四列中的“默认值”将与其他所有内容一起被删除。 如果您只想清除A到C列,并在D列中保留“默认值”,则将该行更改为,

 Sheet11.Cells(1, 1).Resize(1, 3).EntireColumn.ClearContents 

这不会清除整个工作表; 只有列A:C。