Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 将文本文件导入xlsm,然后覆盖sheetname(如果已存在)_Vba_Excel - Fatal编程技术网

Vba 将文本文件导入xlsm,然后覆盖sheetname(如果已存在)

Vba 将文本文件导入xlsm,然后覆盖sheetname(如果已存在),vba,excel,Vba,Excel,在谷歌搜索了数小时失败的VB脚本后,我想我会来这里 目前,我修改了一个VBA脚本,该脚本导入多个txt文件,并根据txt文件名将它们复制到XLSM文件中的新图纸中 我想做两件事,谷歌上的{solved}答案似乎对我不起作用 1) 覆盖现有工作表(如果它已经存在)--(注意:不要删除它…它将链接到另一个工作表进行计算),以及 2) 以空格分隔的格式导入文本文件---同样,不要玩游戏 谢谢(ps——这里有几个类似的问题,有些问题的答案与我的问题相似,但似乎更复杂……我想要的是尽可能简单的东西) Su

在谷歌搜索了数小时失败的VB脚本后,我想我会来这里

目前,我修改了一个VBA脚本,该脚本导入多个txt文件,并根据txt文件名将它们复制到XLSM文件中的新图纸中

我想做两件事,谷歌上的{solved}答案似乎对我不起作用

1) 覆盖现有工作表(如果它已经存在)--(注意:不要删除它…它将链接到另一个工作表进行计算),以及

2) 以空格分隔的格式导入文本文件---同样,不要玩游戏

谢谢(ps——这里有几个类似的问题,有些问题的答案与我的问题相似,但似乎更复杂……我想要的是尽可能简单的东西)

Sub-GetSheets()
Path=“C:\test\”
Filename=Dir(路径&“*.txt”)
文件名“”时执行此操作
工作簿。打开文件名:=路径和文件名,只读:=真
对于ActiveWorkbook.Sheets中的每个工作表
Sheet.Copy After:=此工作簿.Sheets(1)
下一页
工作簿(文件名)。关闭
Filename=Dir()
环
端接头

我不久前遇到了同样的问题,与您的第一个问题有着类似的限制: -保留图纸,因为引用指向此处

然而,我没有你的第二个限制,因此我无法判断它是否符合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
您可能需要更改一些查询选项以满足您的需要

注意:最后三行用于修复一个bug(或者看起来像bug的东西):查询执行不会触发裁判的“计算”事件

' @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