1
目標
這個任務的目標是:VBScript中 - 自定義排序的Excel電子表格並保存
- 在目錄中查找最近的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添加了錯誤信息。 – amanda52