VBScript-自定义对Excel电子表格排序并保存 目标
本任务的目标是:VBScript-自定义对Excel电子表格排序并保存 目标,excel,vbscript,Excel,Vbscript,本任务的目标是: 在目录中查找最新的Excel文件 使用自定义列表(在下面的代码中定义)对该文件进行排序 以相同的名称保存已排序的文件,然后 让Windows计划程序每10分钟运行一次VB脚本 我的尝试 我已经编译了这段代码,但在尝试运行它时收到一条错误消息: 'Find the most recent file in directory Option Explicit Dim fso, path, file, recentDate, recentFile, objExcel, objWor
'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 CompliationError您正在使用VBS无法理解的语法。VBS只提供位置参数,不提供命名参数。此外,Excel常量对外部程序不可用
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
因此,所有这些常量都需要在Excel的对象浏览器中查找并使用值
Const xlSortNormal = 0
Member of Excel.XlSortDataOption
你可以查其他的
假设你的立场是正确的
objWorksheet.Sort.SortFields.Add Range("A1:A20"), Enter Value, Enter Value, "alpha,bravo,charlie,delta,echo,foxtrot,golf,hotel,india,juliet", 0
我在这里的答案有VBA提示和指向Excel提示页面的链接。
也可以在下载VBS帮助。我建议您在问题中添加错误消息。如果没有它,回答这个问题是非常困难的。@JohnWheal添加了错误消息。