Reputation: 117
I am having this problem when running my vba script. It is supposed to open up 4 excel files and one template with a macro. Then it activate the macro and populate each table with vlookup to the specific excel file. The 2 sheets have 3 columns, 2 columns are looking for 1 excel file and the 3rd column looks for the 2nd excel file. But when i run the script, the first 2 columns are #NA whereas the 3rd column has the file directory to the excel file. When i do it manually(Manually open all 4 excel files and click on a button to run the macro), it successfully populate all 3 columns for both sheets. What could be the problem? I searched around and vlookup searches for excel files that are opened, thats why i open the 4 excel files before running the macro to populate the tables. This is my VBA Script:
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
The first Workbooks.open is suppose to open the excel files to populate the first 2 columns, whereas the 2nd Workbooks.Open is to populate the 3rd column. Only the 3rd column gets populated. 3rd column has the file directory and is populated: 1st and 2nd column unable to be found
Upvotes: 2
Views: 191
Reputation: 54757
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
Upvotes: 1
Reputation: 7759
You creating multiple instances of Excel. Eliminate all but the first CreateObject("Excel.Application")
'
Set xlApp = CreateObject("Excel.Application")
Upvotes: 1