谁帮忙优化一下vba代码,excel表格数据有3万行,要执行很长一段时间。

Dim j As Integer, k As Integer, m As Integer, n As Integer
Dim num As Long
If [xfd2].End(xlToLeft) = 0 Then
[a2].EntireRow.Delete
End If
Application.ScreenUpdating = False
Columns("A:B").Insert shift:=xlToRight
[a1] = "编码"
[b1] = "名称"
j = Application.WorksheetFunction.Match("客户编码", Range("1:1"), 0)
m = Application.WorksheetFunction.Match("客户名称", Range("1:1"), 0)
k = Application.WorksheetFunction.Match("门店编码", Range("1:1"), 0)
n = Application.WorksheetFunction.Match("门店名称", Range("1:1"), 0)
For num = 2 To [c65536].End(xlUp).Row Step 1
If Cells(num, k) = "" Then
Cells(num, j).Copy Cells(num, 1)
Cells(num, m).Copy Cells(num, 2)
Else
Cells(num, 1) = Cells(num, j) & "-" & Cells(num, k)
Cells(num, n).Copy Cells(num, 2)
End If
Next
Application.ScreenUpdating = True

第1个回答  2012-08-17
For num = 2 To [c65536].End(xlUp).Row Step 1
If Cells(num, k) = "" Then
Cells(num, j).Copy Cells(num, 1)
Cells(num, m).Copy Cells(num, 2)
Else
Cells(num, 1) = Cells(num, j) & "-" & Cells(num, k)
Cells(num, n).Copy Cells(num, 2)
End If
Next
Application.ScreenUpdating = True
把上面这段循环改一下:

For num = 2 To [c65536].End(xlUp).Row Step 1
If Cells(num, k) = "" Then
Cells(num, 1)=Cells(num, j)
Cells(num, 2)=Cells(num, m)
Else
' 不明白第J列和K列是数字还是文字,如果是数据,就直接用减号,不需要用&,也不要""
Cells(num, 1) = Cells(num, j) & "-" & Cells(num, k)
Cells(num, 2)=Cells(num, n)
End If
Next
Application.ScreenUpdating = True本回答被提问者和网友采纳
第2个回答  2012-08-17
我觉得
For num = 2 To [c65536].End(xlUp).Row Step 1
太费时间了
因为,每次到这里都要计算终值

如果改成在此句之前就求出
[c65536].End(xlUp).Row
赋给一个变量
可能会好一些

如果for。。。next中有EntireRow.Delete
则可以倒序循环

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网