我想利用全局脚本VB来实现等时间段将所需的数据写入EXCEL指定的单元格内,例如:我想当变量TAG1等于1时开始,每隔半个小时将TAG2变量的值写入EXCEL的(1,1)单元格内,我想问下这个每隔半个小时怎么弄,还需要设置触发器之类的什么吗?我也是刚熟悉VB,直接上手困难点。。。望高手们能赐教程序,谢谢!
最佳答案
触发时间自己定义 脚本如下:
Option Explicit
Function action
On Error Resume Next
Dim objExcelApp,oWorkBook,fs,iBlankLine
Set objExcelApp = GetObject(,"Excel.Application")
If VarType(objExcelApp)<>vbEmpty Then
objExcelApp.ActiveWorkbook.Save
Set oWorkBook = objExcelApp.Workbooks.Open("d:\每小时记录.xls")
iBlankLine = oWorkBook.SheetS(1).Columns(1).Find("").Row
objExcelApp.sheets(1).Cells(iBlanKLine, 1).Value = Now()''HMIRuntime.Tags("date").Read
objExcelApp.sheets(1).Cells(iBlanKLine, 2).Value = HMIRuntime.Tags("@CurrentUser").Read
objExcelApp.ActiveWorkbook.Save
Else
Set fs =CreateObject("Scripting.FileSyStemObject")
Set objExcelApp = CreateObject("Excel.Application")
objEXcelApp.VIsible = False
If fs.FileExiSts("d:\每小时记录.xLs") then
Set oWorkBook = objExcelApp.Workbooks.Open("d:\每小时记录.xls")
iBlankLine = oWorkBook.SheetS(1).Columns(1).Find("").Row
objExcelApp.sheets(1).Cells(iBlanKLine, 1).Value = Now()''HMIRuntime.Tags("date").Read
objExcelApp.sheets(1).Cells(iBlanKLine, 2).Value = HMIRuntime.Tags("@CurrentUser").Read
Else
Set oWorkBook =objExcelApp.WorkBooks.Add
oWorkBook.SaveAs "d:\每小时记录.xls "
Set oWorkBook = objExcelApp.Workbooks.Open("d:\每小时记录.xls")
objExcelApp.Workbooks.Open ( "d:\每小时记录.xls" )
objExcelApp.sheets(1).CeLls(1, 1).Value ="时间"
objExcelApp.sheets(1).CeLls(1, 2).vaLue ="用户"
End If
objExcelApp.ActiveWorkbook.Save
objExcelApp.Workbooks.Close
obJexcelApp.Quit
Set objExcelApp = Nothing
End If
End Function
提问者对于答案的评价:
谢谢!
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc263890.html