【发布时间】:2016-12-24 21:16:49
【问题描述】:
目标
这项任务的目标是:
- 在目录中查找最新的Excel文件,
- 使用自定义列表(在下面的代码中定义)对该文件进行排序,
- 以相同的名称保存排序后的文件,然后
- 让 Windows 调度程序每 10 分钟运行一次 VB 脚本。
我的尝试
我已经编译了这段代码,但是当我尝试运行它时收到一条错误消息:
'Find the most recent file in directory
Option Explicit
Dim fso, path, file, recentDate, recentFile, objExcel, objWorkbook, objRange, objWorksheet, SortCol
Set fso = CreateObject("Scripting.FileSystemObject")
Set recentFile = Nothing
For Each file in fso.GetFolder("C:\Users\joe\Documents\test\").Files
If (recentFile is Nothing) Then
Set recentFile = file
ElseIf file.DateLastModified > recentFile.DateLastModified Then
If Left(recentFile.name, 2) = "~$" Then
Set recentFile = file
End If
End If
Next
'Open the most recent file
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(recentFile.Path)
Set objWorksheet = objWorkbook.Worksheets("Sheet1")
'Sort by custom array
objWorksheet.Sort.SortFields.Clear
objWorksheet.Sort.SortFields.Add key:=Range("A1:A20") _
, SortOn:=xlSortOnValues, Order:=xlDescending, CustomOrder:= _
"alpha,bravo,charlie,delta,echo,foxtrot,golf,hotel,india,juliet", DataOption _
:=xlSortNormal
With objWorksheet.Sort
.SetRange Range("A1:B20")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Save Excel workbook with same name
objWorkbook.save
objExcel.quit
错误信息
线路:23
字符:38
错误:预期的语句
代码:800A0400
来源:Microsoft VBScript 编译错误
【问题讨论】:
-
我建议您将错误消息添加到问题中。没有它就很难回答这个问题。
-
@JohnWheal 添加了错误消息。