前段时间我遇到了同样的问题,与你的第一个类似的约束: - 因为引用指向那里,所以保留工作表
但是我没有您的第二个约束,因此我无法判断它是否匹配 100% ;但是我很确定你可以解决它。我什至建议您对临时工作表执行导入查询,然后使用复制粘贴宏操作将您定义明确的范围移动到其最终目的地
我使用导入查询解决了它。我使用“宏记录器”来进行“csv导入”;然后我重构了代码。
' @brief ImportFile : Opens specified file and imports contents at destination
' @param ImpFileName : Path to the file to import
' @param ImpDest : Location of the destination (must be a single cell range)
Private Sub ImportFile(ImpFileName As String, ImpDest As Range)
With ImpDest.Worksheet.QueryTables.Add(Connection:= _
"TEXT;" & ImpFileName, Destination:=ImpDest)
.Name = "Import"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
' As the query execution does not trigger "content change event", we force triggering
' by editing the 1st cell's content.
Dim MyVal As Variant
MyVal = ImpDest.Cells(1, 1).Value
ImpDest.Cells(1, 1) = MyVal
End Sub
您可能想要更改一些查询选项以满足您的需要。
注意:最后三行用于修复错误(或看起来像错误的东西):查询执行不会触发裁判的“计算”事件。