Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
12
返回列表 发新帖
楼主: lhj323323

[已解决][求助]请HYY514老师看看,下标越界了。谢谢蓝桥老师和枯禅老师,第二、按索引导入

[复制链接]
 楼主| 发表于 2010-2-25 22:01 | 显示全部楼层

回复:(蓝桥玄霜)回复:(lhj323323)[求助]第二、求...

QUOTE:
以下是引用蓝桥玄霜在2010-2-23 12:01:00的发言:

Sub 定向导入2()
    Dim Sht1 As Worksheet, Sht2 As Worksheet
    Dim arr, arr1, Arr2, t
    Dim Myr1&, Myr2&, h&, k&
    Set Sht1 = Sheets("数据源")
    Set Sht2 = Sheets("结果表")
    t = Timer
    Application.ScreenUpdating = False
    Myr1 = Sht1.Range("a65536").End(xlUp).Row
    Myr2 = Sht2.Range("a65536").End(xlUp).Row
    arr = Sht1.Range("a2:c" & Myr1)
    arr1 = Sht2.Range("a2:c" & Myr2)
    ReDim Arr2(1 To Myr2 - 1, 1 To 2)
    For h = 1 To UBound(arr1)
        For k = 1 To UBound(arr)
            If arr1(h, 1) = arr(k, 1) Then
                Arr2(h, 1) = arr(k, 3) / 10000
                Arr2(h, 2) = arr(k, 3) / 10000 - arr1(h, 3)
                Exit For
            End If
        Next k
    Next h
    Sht2.Range("d2:e" & Myr2).ClearContents
    Sht2.Range("d2:e" & Myr2) = Arr2
    Application.ScreenUpdating = True
    Sheets("操作面").Range("j6") = Timer - t
    MsgBox Timer - t
End Sub
1,声明变量类型用as Long(简写&),适用数据量大的情况;

2,用数组经过优化,只需要0.34秒。

谢谢蓝桥老师,您说的优化思路我记下来了。谢谢。

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
回复

使用道具 举报

发表于 2010-2-26 04:56 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2010-2-27 00:39 | 显示全部楼层

QUOTE:
以下是引用HYY514在2010-2-25 21:48:00的发言:
Sub ff()
Dim Datar As Variant
Dim AsDat As Variant
Dim ReDat() As Double
Dim Dc As Object
Dim i As Long
Dim tmp As Long
Set Dc = CreateObject("scripting.dictionary")
With Sheet3
Datar = .Range(.Range("a2"), .Range("c65536").End(xlUp))
End With
With Sheet2
AsDat = .Range(.Range("a2"), .Range("c65536").End(xlUp))
End With
For i = 1 To UBound(AsDat)
    Dc(AsDat(i, 1)) = i
Next
ReDim ReDat(1 To UBound(Datar), 1 To 2)
For i = 1 To UBound(Datar)
   tmp = Dc(Datar(i, 1))
   'Debug.Print tmp
   ReDat(i, 1) = AsDat(tmp, 3) / 10000
   ReDat(i, 2) = AsDat(tmp, 3) / 10000 - Datar(i, 3)
Next
Sheet3.Range("d2").Resize(UBound(ReDat), 2) = ReDat
MsgBox "ok!"
End Sub

HYY514老师

谢谢关注,

不过ReDat(i, 1) = AsDat(tmp, 3) / 10000提示下标越界了

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-6 14:01 , Processed in 0.242709 second(s), 8 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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