为什么一个简单的excel宏的程序要运行7分钟呢?
我想取两个表的交集 用宏写了个程序,但运行特别慢.两个表都只有300行.请大家帮忙了!下面是我的程序.
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlSheet As Worksheet
sheetname = Sheet1.Name
s = ActiveWorkbook.Path
Name = ActiveWorkbook.Name
strlen = Len(Name)
p = Right(Name, 4)
Sheet1.Name = sheetname
n = s + "\名单" + p
Set xlapp = New Excel.Application
Set xlbook = xlapp.Workbooks.Open(n)
Set xlSheet = xlbook.Worksheets(1)
i = 4
j = 3
Do While (Sheet1.Cells(i, 2) <> "")
a = Sheet1.Cells(i, 2)
a = Trim(a)
j = 3
Do While (xlSheet.Cells(j, 2) <> "")
b = xlSheet.Cells(j, 2)
b = Trim(b)
If (Mid(b, Len(b), 1) = ")") Then '去掉后面的括号
b = Mid(b, 1, Len(b) - 3)
End If
If ((Mid(b, Len(b), 1) = "▲") Or (Mid(b, Len(b), 1) = "※")) Then '去掉后面的▲※
b = Mid(b, 1, Len(b) - 1)
End If
'b是职务表,当名字是两个字的时候中间有空格,一个或多个
flag = ""
For m = 1 To Len(b)
e = Mid(b, m, 1)
If (e = " ") Then
flag = "y"
Exit For
End If
Next
If (flag = "y") Then
If (Len(b) = 3) Then
b = Mid(b, 1, 1) + Mid(b, Len(b) - 1, 1)
End If
End If
If (a = b) Then
Sheet1.Cells(i, 7) = xlSheet.Cells(j, 4) '''''''''''''新数据写入位置
Exit Do
End If
j = j + 1
Loop
i = i + 1
Loop
Sheet1.Cells(i + 2, 1) = "完成"
xlbook.Close
--------------------编程问答-------------------- 你这样做肯定慢撒,先把两个表的数据一次性装入内存,在内存里进行运算,运算完之后,再一次性写入Excel,速度奇快,我估计不会超过2秒钟!我以前把一个6万行数据的Excel表,转化成非常复杂的报表,只用了1秒的时间,就是滴答一下,报表就出来了!你这样写Excel对象要调用300*300=90000次,速度不慢才怪!
补充:.NET技术 , ASP.NET