私はあなたの最初と同様の制約で、少し前に同じ問題に直面しました: - 参照がそこを指しているため、シートを保持します
ただし、2 番目の制約がなかったため、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
必要に応じて、クエリ オプションの一部を変更することができます。
注: 最後の 3 行は、バグ (またはバグのように見えるもの) を修正するためにあります。クエリの実行は、レフリーで「計算」イベントをトリガーしません。