Hello all,
I am running a VBS macro in our ERP that opens a target Excel Document and populates the form with data from a workbook. The program works but the execution time is roughley 1 and a half minutes. I placed some MsgBox's throughout the script to see where the lag was comming from, and the entire script executes in about 4 to 5 seconds, but when watching active processes in Task Manager, I can see that Execl remains open for roughley 1 and a half minutes, and when it does close, the ERP application refreshes and populates with the data.
I think I may be using some inefficent code in closing Excel. I know this is a VBS question, not VBA but I hope this is still an adaquate forum as it pertains to Excel. Any help will be appreciated, I have placed my code below.
I am running a VBS macro in our ERP that opens a target Excel Document and populates the form with data from a workbook. The program works but the execution time is roughley 1 and a half minutes. I placed some MsgBox's throughout the script to see where the lag was comming from, and the entire script executes in about 4 to 5 seconds, but when watching active processes in Task Manager, I can see that Execl remains open for roughley 1 and a half minutes, and when it does close, the ERP application refreshes and populates with the data.
I think I may be using some inefficent code in closing Excel. I know this is a VBS question, not VBA but I hope this is still an adaquate forum as it pertains to Excel. Any help will be appreciated, I have placed my code below.
Code:
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
Dim yr
yr = InputBox("Please enter the payroll year")
Dim wk
wk = InputBox("Please enter the payroll week")
Dim path
path = "K:\Staff Accountant 2\3. Payroll\1. Payroll JE's\" & yr & "\Hourly\WK " & wk & "\RZU Payroll Template.xlsx"
Function FileExists(FilePath)
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(FilePath) Then
FileExists=CBool(1)
Else
FileExists=Cbool(0)
End If
End Function
If FileExists(path) Then
Dim objExcel, objWorkbook, objSheet
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(path)
Set objSheet = objExcel.ActiveWorkbook.Worksheets(3)
Dim row
row = 3
Do While row <=28
val1 = objSheet.Cells(row,2).value
val2 = objSheet.Cells(row,4).value
val3 = objSheet.Cells(row,5).value
WshShell.SendKeys "{INSERT}"
WshShell.SendKeys val1
WshShell.SendKeys "{TAB}"
WshShell.SendKeys val2
WshShell.SendKeys "{TAB}"
WshShell.SendKeys val3
row = row + 1
Loop
objExcel.ActiveWorkbook.Close
objExcel.Workbooks.Close
objExcel.Application.Quit
Set objExcel = Nothing
Else
MsgBox("Your entries resulted in an invalid File Path. Please check the file location and try again")
End If
MsgBox(val1)