SliderJeff
Board Regular
- Joined
- Oct 5, 2016
- Messages
- 63
Hey gang,
So I've noticed that I am evidently doing something incorrectly when I am invoking a particular bit of VBA code within a source.XLSM file. Here's my program file flow.
script.BAT calls script.VBS which calls "ProcessCVS" subroutine within script.XLSM.
Here are the pertinent open/close/save lines of each of these files.
script.BAT
script.vbs
script.xlsm
I thought I had covered all my bases as far as closing the various different XLSM files, BAT files, etc... by pointing things to "Nothing" and such, but apparently not. Can anyone see where I may have missed closing something correctly?
What I can tell you from my observations is that the macro code is clearly closing my script.xlsm file, as it disappears from the VBE one I single step past the
line of code. I also see the protect file with the "~$..." naming convention disappear from the working directory that all of the files are located within.
I'd appreciate any help you can provide.
Thanks,
Jeff
So I've noticed that I am evidently doing something incorrectly when I am invoking a particular bit of VBA code within a source.XLSM file. Here's my program file flow.
script.BAT calls script.VBS which calls "ProcessCVS" subroutine within script.XLSM.
Here are the pertinent open/close/save lines of each of these files.
script.BAT
Code:
@echo off
pushd %~dp0
rem Command Console Script - Feedback is in command window only
cscript script.vbs
script.vbs
Code:
Option Explicit
On Error Resume Next
DrillDataScripting
Sub DrillDataScripting()
Dim xlApp
Dim xlBook
Dim WshShell, strCurDir
Set WshShell = CreateObject("WScript.Shell")
strCurDir = WshShell.CurrentDirectory
Set WshShell = Nothing
Set xlApp = CreateObject("Excel.Application")
' Make Excel visible through the Application object
xlApp.Application.Visible = True
Set xlBook = xlApp.Workbooks.Open(strCurDir & "\Script.xlsm")
xlApp.Run "ProcessCSV"
' Close the XLSM file with VBA code in it and don't save changes
xlBook.Close(False)
Set xlApp = Nothing
Set xlBook = Nothing
End Sub
script.xlsm
Code:
Sub ProcessDrillDataCSV()
Dim wbScript As Workbook
' Variable to hold source Workbook file
Dim wbOpen As Workbook
' Variable to hold destination output Workbook file for all processing
Dim wbNew As Workbook
' Turn on multithreading
Application.MultiThreadedCalculation.Enabled = True
Set wbScript = ActiveWorkbook
' Change to the appropriate directory with the files
ChDir strPath
' List all of the CSV files in the current path folder
strExtension = Dir("*.csv")
' Create the new workbook to store all of the statistical info and charts
Set wbNew = Workbooks.Add(1)
'Change Path, Name and File Format to macro-enabled XLSM Excel workbook
wbNew.SaveAs fileName:=Format(DateTime.Now, "yyyyMMdd hh-mm-ss") & " Output Dashboard", FileFormat:=xlOpenXMLWorkbookMacroEnabled
' Main loop to process files
Do While strExtension <> ""
Set wbOpen = Workbooks.Open(strPath & "\" & strExtension)
With wbOpen
Call formatDateAndResizeColumns
Call FindExtremeValues(fileCount, Sheets(1).Name, wbNew, wbOpen, wbScript)
Call FindStatValues(fileCount, wbOpen.Sheets(1).Name, wbNew, wbOpen, wbScript)
.Close SaveChanges:=False
End With
' Go to next CSV file in list
strExtension = Dir
Loop
' Make the "All Stats" sheet the active sheet
Sheets("All Stats").Activate
' Set selection to cell A1 so that the upper left corner of the sheet is set to view
Range("A1").Select
' Re-enable alerts
Application.DisplayAlerts = True
' Save the file as it's currently opened name
wbNew.Save
' Close the script file, itself, without saving
wbScript.Close (False)
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
On Error GoTo 0
End Sub
What I can tell you from my observations is that the macro code is clearly closing my script.xlsm file, as it disappears from the VBE one I single step past the
Code:
wbScript.Close (False)
I'd appreciate any help you can provide.
Thanks,
Jeff