Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2511|回复: 5

[已解决]代码运行速度慢,有没有更好的方法?

[复制链接]
发表于 2014-10-11 16:39 | 显示全部楼层 |阅读模式
本帖最后由 wling1228 于 2014-10-16 16:04 编辑

希望达到的效果是:将“逾期明细表0925”中的数据复制到后面的“today”sheet,并且保留最大日期对应的“类型”栏的值,其它“类型”中的值都删除。

写了代码如下,但是觉得运行太慢,请问还有没有更高效的方法?谢谢!
Sub copy()
Dim x As Integer
Dim D As Date
'Dim t As String
'D = Year(Now() - 1) & "/" & Month(Now() - 1) & "/" & Day(Now() - 1)

Sheets("逾期明细表0925").Activate
x = Cells(Rows.Count, "a").End(xlUp).Row
ActiveSheet.Range("A2:Y" & x).Select
Selection.copy
Sheets("today").Activate
ActiveSheet.Range("a2").Select
ActiveSheet.Paste
D = Application.WorksheetFunction.Max(Range("l3:l" & x))
ActiveSheet.Range("A2:AD" & x).AutoFilter Field:=12, Criteria1:="<>" & D, Operator:=xlFilterValues
With Range("y3:y" & x)
.SpecialCells(xlCellTypeVisible).Value = ""
.EntireRow.Hidden = False
End With
End Sub
最佳答案
2014-10-13 10:56
  1. Sub copy()
  2.     Dim x As Integer
  3.     Dim D As Date
  4.     Dim Sh As Worksheet
  5.     Set Sh = Sheets("Today")
  6.     With Sheets("逾期明细表0925")
  7.         x = .Cells(.Rows.Count, "a").End(xlUp).Row
  8.         .Range("A2:Y" & x).copy Sh.Range("a2")
  9.     End With
  10.     D = Application.WorksheetFunction.Max(Sh.Range("l3:l" & x))
  11.     Sh.Range("A2:AD" & x).AutoFilter Field:=12, Criteria1:="<>" & D, Operator:=xlFilterValues
  12.     With Sh.Range("y3:y" & x)
  13.         .SpecialCells(xlCellTypeVisible).Value = ""
  14.         .EntireRow.Hidden = False
  15.     End With
  16. End Sub
复制代码

test.rar

87.14 KB, 下载次数: 13

附件

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2014-10-11 18:53 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2014-10-13 09:48 | 显示全部楼层
qh8600 发表于 2014-10-11 18:53
像Select这种多了,肯定慢的

谢谢,那有其它更好的方法吗?是不是可以不用select呢?
回复

使用道具 举报

发表于 2014-10-13 10:04 | 显示全部楼层
wling1228 发表于 2014-10-13 09:48
谢谢,那有其它更好的方法吗?是不是可以不用select呢?

像这种的
ActiveSheet.Range("A2:Y" & x).Select
Selection.copy
可以简化为
ActiveSheet.Range("A2:Y" & x).copy

Sheets("逾期明细表0925").Activate这个不需要
直接用
with Sheets("逾期明细表0925")
.Range("A2:Y" & x).copy
........
end with

可以快不少
回复

使用道具 举报

发表于 2014-10-13 10:56 | 显示全部楼层    本楼为最佳答案   
  1. Sub copy()
  2.     Dim x As Integer
  3.     Dim D As Date
  4.     Dim Sh As Worksheet
  5.     Set Sh = Sheets("Today")
  6.     With Sheets("逾期明细表0925")
  7.         x = .Cells(.Rows.Count, "a").End(xlUp).Row
  8.         .Range("A2:Y" & x).copy Sh.Range("a2")
  9.     End With
  10.     D = Application.WorksheetFunction.Max(Sh.Range("l3:l" & x))
  11.     Sh.Range("A2:AD" & x).AutoFilter Field:=12, Criteria1:="<>" & D, Operator:=xlFilterValues
  12.     With Sh.Range("y3:y" & x)
  13.         .SpecialCells(xlCellTypeVisible).Value = ""
  14.         .EntireRow.Hidden = False
  15.     End With
  16. End Sub
复制代码
回复

使用道具 举报

发表于 2014-10-13 11:02 | 显示全部楼层
就复制那么一盘,又能慢到哪里去?如果真慢,那说明有其它问题存在,而不是你选了几回的问题
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-20 06:49 , Processed in 0.337728 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表