运行vba脚本时出现此问题。它应该打开4个excel文件和一个带有宏的模板。然后,它激活宏,并使用vlookup填充每个表到特定的excel文件。 2张纸有3列,其中2列正在寻找1个excel文件,第3列正在寻找第二个excel文件。但是,当我运行脚本时,前2列是#NA,而第3列具有指向excel文件的文件目录。当我手动执行此操作时(手动打开所有4个excel文件并单击按钮以运行宏),它成功填充了两个工作表的所有3列。可能是什么问题呢?我到处搜索,然后vlookup搜索打开的excel文件,这就是为什么我在运行宏以填充表之前打开4个excel文件的原因。 这是我的VBA脚本:
Option Explicit
Dim xlApp, xlBook, shell
Dim folderPath
' Set shell = CreateObject("WScript.Shell")
' shell.Run "C:\Users\NLTAdmin\Desktop\Extraction_Automation\TABLE_COUNT_SCRIPT\All_in_one.bat", 1, True
folderPath = "C:\Users\NLTAdmin\Desktop\Extraction_Automation\TABLE_COUNT_SCRIPT\"
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.DisplayAlerts = False
xlApp.Workbooks.Open(folderPath & "Assetco\Assetco.csv")
xlApp.Workbooks.Open(folderPath & "BIOS\BIOS.csv")
' xlApp.Workbooks.Open(folderPath & "FDS\FDS.csv")
' xlApp.Workbooks.Open(folderPath & "FTTH\FTTH.csv")
' xlApp.Workbooks.Open(folderPath & "FTTH_Filter\ftth_union_filter_queries.csv")
' xlApp.Workbooks.Open(folderPath & "Gtech_Filter\Gtech_filter_queries.csv")
' xlApp.Workbooks.Open(folderPath & "Gtechv1\Gtech.csv")
' xlApp.Workbooks.Open(folderPath & "MARS\MARS.csv")
' xlApp.Workbooks.Open(folderPath & "MARS_Filter\mars_filter_queries.csv")
' xlApp.Workbooks.Open(folderPath & "WOMS\WOMS.csv")
' xlApp.Workbooks.Open(folderPath & "Workflow\WORKFLOW.csv")
' xlApp.Workbooks.Open(folderPath & "Workflow_Filter\workflow_filter_queries.csv")
folderPath = "C:\Users\NLTAdmin\Desktop\Extraction_Automation\Union_queries\"
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.DisplayAlerts = False
xlApp.Workbooks.Open(folderPath & "ASSETCO_union.csv")
xlApp.Workbooks.Open(folderPath & "BIOS_union.csv")
' xlApp.Workbooks.Open(folderPath & "FDS_union.csv")
' xlApp.Workbooks.Open(folderPath & "FTTH_union.csv")
' xlApp.Workbooks.Open(folderPath & "GTECH_union.csv")
' xlApp.Workbooks.Open(folderPath & "MARS_union.csv")
' xlApp.Workbooks.Open(folderPath & "WOMS_union.csv")
' xlApp.Workbooks.Open(folderPath & "WORKFLOW_union.csv")
set xlBook = xlApp.Workbooks.Open("C:\Users\NLTAdmin\Desktop\Extraction_Automation\extraction_report_template.xlsm",0,True)
xlApp.Run "Macro1"
xlApp.DisplayAlerts = True
xlApp.Quit
Set xlApp = Nothing
WScript.Quit
第一个Workbooks.open是假设要打开excel文件以填充前2列,而第2个Workbooks.Open是要填充第三列。仅填充第三列。 第三列具有文件目录并被填充: 找不到第一列和第二列
答案 0 :(得分:1)
您创建多个Excel实例。消除除第一个CreateObject("Excel.Application")
'
Set xlApp = CreateObject("Excel.Application")
答案 1 :(得分:1)
Option Explicit
Dim xlApp, xlBook, shell
Dim folderPath
' Set shell = CreateObject("WScript.Shell")
' shell.Run "C:\Users\NLTAdmin\Desktop\Extraction_Automation\ _
' & "TABLE_COUNT_SCRIPT\All_in_one.bat", 1, True
folderPath = "C:\Users\NLTAdmin\Desktop\Extraction_Automation\" _
& "TABLE_COUNT_SCRIPT\"
' To avoid multiple instances of Excel, when it is running,
' the GetObject Method has to be used, but when Excel is not running
' the CreateObject Method has to be used to open it.
On Error Resume Next
' When Excel is running:
Set xlApp = GetObject(, "Excel.Application") ' Excel is running
If Err Then
Set xlApp = CreateObject("Excel.Application") ' Excel not running.
End If
On Error GoTo 0
With xlApp
.Visible = True
.DisplayAlerts = False
.Workbooks.Open (folderPath & "Assetco\Assetco.csv")
.Workbooks.Open (folderPath & "BIOS\BIOS.csv")
' .Workbooks.Open(folderPath & "FDS\FDS.csv")
' .Workbooks.Open(folderPath & "FTTH\FTTH.csv")
' .Workbooks.Open(folderPath & "FTTH_Filter\ftth_union_filter_queries.csv")
' .Workbooks.Open(folderPath & "Gtech_Filter\Gtech_filter_queries.csv")
' .Workbooks.Open(folderPath & "Gtechv1\Gtech.csv")
' .Workbooks.Open(folderPath & "MARS\MARS.csv")
' .Workbooks.Open(folderPath & "MARS_Filter\mars_filter_queries.csv")
' .Workbooks.Open(folderPath & "WOMS\WOMS.csv")
' .Workbooks.Open(folderPath & "Workflow\WORKFLOW.csv")
' .Workbooks.Open(folderPath & "Workflow_Filter\workflow_filter_queries.csv")
folderPath = "C:\Users\NLTAdmin\Desktop\Extraction_Automation\" _
& "Union_queries\"
.Workbooks.Open (folderPath & "ASSETCO_union.csv")
.Workbooks.Open (folderPath & "BIOS_union.csv")
' .Workbooks.Open(folderPath & "FDS_union.csv")
' .Workbooks.Open(folderPath & "FTTH_union.csv")
' .Workbooks.Open(folderPath & "GTECH_union.csv")
' .Workbooks.Open(folderPath & "MARS_union.csv")
' .Workbooks.Open(folderPath & "WOMS_union.csv")
' .Workbooks.Open(folderPath & "WORKFLOW_union.csv")
Set xlBook = .Workbooks.Open(folderPath _
& "extraction_report_template.xlsm", 0, True)
.Run "Macro1"
.DisplayAlerts = True
.Quit
End With
Set xlApp = Nothing
WScript.Quit