donthate918
New Member
- Joined
- Sep 19, 2017
- Messages
- 14
Okay, I have these codes that do exactly what I want when I run them manually:
1. Merges all the reports into one workbook
2. Saves the file to the location I want.
But when I try to run this .vbs it throws an error on the last line of the "SaveFileAs" line of code
I can run the .vbs in a macro in a blank workbook and it works just fine.
I want to set a task in scheduler to run this for me.
Any ideas? Also, any help is greatly appreciated.
1. Merges all the reports into one workbook
Code:
Sub MergeAllWorkbooks()
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim NRow As Long
Dim FileName As String
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range
' Create a new workbook and set a variable to the first sheet.
' I want to make this the current workbook,sheet 1
Set SummarySheet = ThisWorkbook.Worksheets("DailyInspectionReports")
' Modify this folder path to point to the files you want to use.
FolderPath = "S:\Quality Control\Reports\Inspection\2017\"
' NRow keeps track of where to insert new rows in the destination workbook.
NRow = 2
' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "*.xl*")
' Loop until Dir returns an empty string.
Do While FileName <> ""
' Open a workbook in the folder
Set WorkBk = Workbooks.Open(FolderPath & FileName)
' Set the cell in column A to be the file name.
' Not using this feature.
' SummarySheet.Range("A" & NRow).Value = FileName
' Set the source range to be cell through cell or range.
' Modify this range for your workbooks.
' It can span multiple rows.
lastRow = WorkBk.Worksheets(1).Cells(Rows.Count, "C").End(xlUp).Row
Set SourceRange = WorkBk.Worksheets(1).Range("A9:J" & lastRow)
' Set the destination range to start at column and
' be the same size as the source range.
' I also want the destination sheet to stay the same size and formatting.
Set DestRange = ThisWorkbook.Worksheets("DailyInspectionReports").Range("A" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)
' Copy over the values from the source to the destination.
DestRange.Value = SourceRange.Value
' Increase NRow so that we know where to copy data next.
NRow = NRow + DestRange.Rows.Count
' Close the source workbook without saving changes.
WorkBk.Close savechanges:=False
' Use Dir to get the next file name.
FileName = Dir()
Loop
' Call AutoFit on the destination sheet so that all
' data is readable.
ThisWorkbook.Worksheets(1).Columns.AutoFit
End Sub
2. Saves the file to the location I want.
Code:
Sub SaveFileAs()
Dim strPath As String
Dim strFolderPath As String
strFolderPath = "S:\Quality Control\Reports\Daily Batch Files\Inspection\"
strPath = strFolderPath & "Daily Inspection Report Summary" & "-" & Format(Now, "mm-dd-yyyy-hh_mm")
ActiveWorkbook.SaveAs FileName:=strPath
End Sub
But when I try to run this .vbs it throws an error on the last line of the "SaveFileAs" line of code
Code:
dim eApp
set eApp = GetObject("S:\Quality Control\Reports\Daily Inspection Report Summary.xlsm")
eApp.Application.Run "'Daily Inspection Report Summary.xlsm'!MergeAllWorkbooks"
eApp.Application.Run "'Daily Inspection Report Summary.xlsm'!SaveFileAs"
set eApp = nothing
I can run the .vbs in a macro in a blank workbook and it works just fine.
I want to set a task in scheduler to run this for me.
Any ideas? Also, any help is greatly appreciated.