我有一张包含以下数据的表格:
现在,Excel总共有36个任务,每个任务都有4列。第一个任务.i.e任务1名称将始终从L列开始。已经描述了36个任务144列。现在我们需要逐行检查并且需要检查TNStart开始日期< T(N + 1)开始日期。然后该行将被选为坏行。简而言之,当任务#号从1增加到36时,开始日期应该按顺序递增。如果随时失败,则应将行标记为错误数据。
你能帮助我在这里以时尚的方式做到这一点吗?
Option Explicit
Dim objExcel1
Dim strPathExcel1
Dim objSheet1,objSheet2
Dim IntRow1,IntRow2
Dim ColStart
Set objExcel1 = CreateObject("Excel.Application")'Object for Condition Dump
strPathExcel1 = "D:\AravoVB\Copy of Original Scripts\CopyofGEWingtoWing_latest_dump_21112012.xls"
objExcel1.Workbooks.Open strPathExcel1
Set objSheet1 = objExcel1.ActiveWorkbook.Worksheets(1)
Set objSheet2 = objExcel1.ActiveWorkbook.Worksheets("Bad Data")
objExcel1.ScreenUpdating = False
objExcel1.Calculation = -4135 'xlCalculationManual
IntRow2=2
IntRow1=4
Do Until IntRow1 > objSheet1.UsedRange.Rows.Count
ColStart = objExcel1.Application.WorksheetFunction.Match("Parent Business Process ID", objSheet1.Rows(3), 0) + 1
Do Until ColStart > objSheet1.UsedRange.Columns.Count And objSheet1.Cells(IntRow1,ColStart) = ""
If objSheet1.Cells(IntRow1,ColStart + 1) > objSheet1.Cells(IntRow1,ColStart + 5) and objsheet1.cells(IntRow,ColStart + 5) <> "" Then
objSheet1.Range(objSheet1.Cells(IntRow1,1),objSheet1.Cells(IntRow1,objSheet1.UsedRange.Columns.Count)).Copy
objSheet2.Range(objSheet2.Cells(IntRow2,1),objSheet2.Cells(IntRow2,objSheet1.UsedRange.Columns.Count)).PasteSpecial
IntRow2=IntRow2+1
Exit Do
End If
ColStart=ColStart+4
Loop
IntRow1=IntRow1+1
Loop
objExcel1.ScreenUpdating = True
objExcel1.Calculation = -4105 'xlCalculationAutomatic
效果不佳
我的工作表有2000行,错误的数据选择标准是144列。现在输出在25分钟后出现。所以它提高了整体性能。因此,我要求你的人帮我做它更快一点。
是否有可能在将错误的行应对到另一张纸时,也标记为红色的坏列
答案 0 :(得分:4)
我建议通过ADODB连接到Excel电子表格,并使用SQL检索数据。然后,您可以使用CopyFromRecordset
方法将数据导出到新的Excel电子表格中。
Option Explicit
Dim conn, cmd, rs
Dim clauses(34), i
Dim xlApp, xlBook
Set conn = CreateObject("ADODB.Connection")
With conn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=""C:\path\to\excel\file.xlsx"";" & _
"Extended Properties=""Excel 12.0;HDR=Yes"""
'If you don't have Office 2007 or later, your connection string should look like this:
'.ConnectionString = "Data Source=""C:\path\to\excel\file.xls"";" & _
' "Extended Properties=""Excel 8.0;HDR=Yes"""
.Open
End With
For i = 0 To 34
clauses(i) = "[Task" & i + 1 & " Start Date] < [Task" & i + 2 & " Start Date]"
Next
Set cmd = CreateObject("ADODB.Command")
cmd.CommandText = "SELECT * FROM [WorksheetName$] WHERE " & Join(clauses, " OR ")
cmd.ActiveConnection = conn
Set rs = cmd.Execute
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
xlBook.Sheets(1).Range("A1").CopyFromRecordset cmd.Execute
将C:\path\to\excel\file.xlsx
和WorksheetName
替换为适当的值。
一些链接:
VBScript / WSH /脚本运行时
ADODB - ActiveX数据对象
MSDN上的许多示例都使用VBA或VB6。有关将VBA / VB6移植到VBScript的简短介绍,请参阅here。要记住的主要问题是大多数这些主题(ADODB,Excel,Scripting Runtime)都不是特定于VBScript的;它们是任何支持COM的语言都可用的对象模型,它们的用法看起来非常相似(有关Python中的示例,请参阅here)。