Option Explicit
'获取Excel文件路径
dim path
path = GetFilePath
'打开excel文档
Dim oExcel
Dim oBook
Call OpenExcel(oExcel, oBook)
'获得所需处理的sheet页
Dim oSheet
Call OpenSheet(oBook, oSheet, "gh")
'处理数据
Dim inRow
For inRow=2 To 5
Dim value
Set value = oSheet.cells(inRow, GetColNumByName(oExcel, oSheet, "name"))
If value = "高" Then
oSheet.cells(inRow, 3) = "OK"
Else
oSheet.cells(inRow, 3) = "NG"
End If
Next
oExcel.Save
oExcel.quit
WScript.Quit
'==================sub & function==================
sub OpenExcel(ByRef obj, ByRef workbook)
On Error Resume Next
'打开excel文档
Set obj = CreateObject("excel.application")
oExcel.Visible = false
Set workbook = obj.Workbooks.Open(path)
If Err.Number <> 0 Then
oExcel.quit
WScript.Quit
End If
End sub
Sub OpenSheet(ByRef book, ByRef sheet, byval name)
Set sheet = book.Sheets(name)
End Sub
Function GetFilePath
Dim objDialog
Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = "Excel File|*.xls"
objDialog.InitialDir = "c:/"
Dim tfile
tfile=objDialog.ShowOpen
if tfile then
GetFilePath = objDialog.FileName
else
WScript.Quit
end if
End Function
'获得处理字段所在列
Function GetColNumByName(ByRef excel, ByRef sheet, ByVal strColName)
GetColNumByName = 0
Dim index
For index = 1 To 10
If sheet.cells(1, index) = strColName Then
GetColNumByName = index
Exit Function
End if
Next
If GetColNumByName = 0 Then
MsgBox("找不到【" & strColName & "】列!")
excel.quit
WScript.Quit
End If
End Function