【问题标题】:Access Running Excel For Text to Columns (Loop multiple times)Access Running Excel For Text to Columns(循环多次)
【发布时间】:2013-09-17 02:50:28
【问题描述】:

我的代码有问题 - 我需要它多次导入数据,但我需要先将数据正确编辑到分隔列中。

基本上,当我踏入它时,程序可以一次性完成列文本。但在那之后,即使这些文本文件的格式都相同,它也无法转换

示例数据 - 如您所见,这些数据有分隔符(= 和 ,)

"7",="AR",="",="",="720325438",="491.45",="0.00",="0.00",="0.00",="0.00"
"7",="AR",="",="",="720344843",="40.00",="0.00",="0.00",="0.00",="0.00"

代码:

Dim xlApp As Excel.Application
Dim xlSheet As Excel.Worksheet


For intCurrentfileNumber = 1 To intNumOfFiles
strCurrentFolderName = varFileArray(intCurrentfileNumber, 0)
strCurrentFileName = varFileArray(intCurrentfileNumber, 1)

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True

xlApp.Workbooks.Open strCurrentFolderName & "\" & strCurrentFileName
Set xlSheet = xlApp.Worksheets(1)

xlSheet.("A1", Range("A" & Rows.Count).End(xlUp)).TextToColumns _
Destination:=Range("A1"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Tab:=False, _
Semicolon:=False, _
Comma:=True, _
Space:=False, _
Other:=True, _
OtherChar:="=", _
TrailingMinusNumbers:=True

ActiveWorkbook.Close (True)
xlApp.Quit
Set xlApp = Nothing
Set xlSheet = Nothing

DoCmd.TransferText acImportDelim, "Import Specification", "Staging", strCurrentFolderName & "\" & strCurrentFileName, True

Next intCurrentFileNumber

【问题讨论】:

  • 失败是什么错误?还是只是做得不好?
  • 我不会在循环中继续启动和退出 Excel。我会做所有的 Excel 自动化,然后退出 Excel(一次),然后执行所有传输。我可能还会将它们分成两个单独的程序。
  • .. 但是,再次查看您的代码,您似乎试图一遍又一遍地导入同一个文件(?)。
  • @Cor_Blimey:只是做得不好。我没有例外。最终结果是大部分文件都没有被编辑。
  • @Andy G - 抱歉,我错过了使用数组更新当前文件夹和当前文件的代码部分。我会看看只打开一次excel可以做什么以及结果会是什么。谢谢。

标签: vba ms-access excel


【解决方案1】:

好吧,我不确定我的错误到底出在哪里 - 我认为错误在这里的某个地方 - 实际上,在关闭并重新打开 Access 之后,它开始给我一个错误并且不让我运行它。

xlSheet.("A1", Range("A" & Rows.Count).End(xlUp)).TextToColumns _

@Andy G - 感谢您关于不关闭/重新打开 Excel 应用程序的建议 - 这可能也是它工作的一部分。然而,这更像是一种“贫民窟”的运行方式——但它似乎运行一切都很好。它打开了所有的导入文本,并相应地调整了 textToColumns,并成功导入。

如果有人有更干净的方法,请随时加入。谢谢!

xlSheet.Columns("A:A").Select
Selection.TextToColumns , _
Destination:=Range("A1"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Tab:=False, _
Semicolon:=False, _
Comma:=True, _
Space:=False, _
Other:=True, _
OtherChar:="=", _
TrailingMinusNumbers:=True

【讨论】:

    猜你喜欢
    • 2016-03-08
    • 2023-03-28
    • 2019-06-20
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    • 2013-10-13
    • 2023-01-04
    • 1970-01-01
    相关资源
    最近更新 更多