此任务的目标是:
我已编译此代码,但在尝试运行时收到错误消息:
'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
Char:38
错误:预期声明
代码:800A0400
来源:Microsoft VBScript compilationerror
答案 0 :(得分:1)
您正在使用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提示的页面链接。 Pointers needed for speeding up nested loop macro in VBA
此外,可以在http://download.microsoft.com/download/winscript56/Install/5.6/W982KMeXP/EN-US/scrdoc56en.exe下载VBS帮助。