简体   繁体   English

Excel VBA在大范围内通过2种条件加速了Vlookup

[英]Excel VBA speed up Vlookup with 2 conditions over a large range

I have a sample of some VBA that lookup a concatenation of 2 columns. 我有一些VBA的示例,该示例查找2列的串联。 This looksup a database feed, with a row count from between 35k and 250k. 这将查找数据库供稿,行数介于35k和250k之间。

Doing vlookups is way too slow with times from 60 to 500 seconds. 在60到500秒的时间内执行vlookups太慢了。 What would be the most efficient way to get the the same result. 获得相同结果的最有效方法是什么。

Sequence 序列

  • Turns of screen updating 屏幕更新次数
  • Turns off all calculations 关闭所有计算
  • Disables database 禁用数据库
  • Clears clipboard cache 清除剪贴板缓存
  • Refreshes db data 刷新数据库数据
  • Sets the lookups 设置查询
  • Turns on calculations 打开计算
  • Turns off calculations 关闭计算
  • Copy and pastes values of the vlookups. 复制并粘贴vlookup的值。
  • Enables database 启用数据库
  • Turns everything back on 重新打开所有内容

S 小号

 Sub startcom()
  Dim ii As Long, lastrow As Long
  Dim StartTime As Double
  Dim SecondsElapsed As Double

' starts timer
     StartTime = Timer

 'freeze screens, clears cache and stops cals
    stopall

'Set error traps and start and end times
     On Error GoTo errortrap:


Set sht1 = wsRag
Set sht2 = wsComdata

sht2.Select
    reflist

'Find the last row (in column A) with data. and set start row for data copy
   lastrow = sht1.Range("A:A").Find("*", SearchDirection:=xlPrevious).Row
ii = 9

'disables db connection
  wsConfig.Cells(7, 2) = 0

sht1.Select

 Range("AM" & ii & ":AM" & lastrow).Formula = "=IF(VLOOKUP(CONCATENATE(A"& ii &",B" & ii &"),Comment_data!A:F,4,0)="""","""",VLOOKUP(CONCATENATE(A" & ii   & ",B" & ii & "),Comment_data!A:F,4,0))" ' Get comments
    calcon
    calcoff
    Range("AM" & ii & ":AM" & lastrow & "").Select
        Selection.Copy
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False

'enable DB connection
    wsConfig.Cells(7, 2) = 1
'Determine how many seconds code took to run
  SecondsElapsed = Round(Timer - StartTime, 2)

'get lenghth of runtime
Debug.Print "Ran successfully in " & SecondsElapsed & " seconds",    vbInformation
startall
  Exit Sub
   errortrap:
     errormess
 Debug.Print "Location: Comments start"
End Sub

The issue 问题

As I understand your issue lies with the VLOOKUP operations , this bit here (spread over a couple of rows to make it more readable): 据我了解,您的问题出在VLOOKUP操作上 ,这是这里(分散了几行以使其更具可读性):

Range("AM" & ii & ":AM" & lastrow).Formula =
    "=IF( 
        VLOOKUP(CONCATENATE(A"& ii &",B" & ii &"),Comment_data!A:F,4,0)="""",
        """",
        VLOOKUP(CONCATENATE(A" & ii   & ",B" & ii & "),Comment_data!A:F,4,0)
    )" ' Get comments

Solution 1 解决方案1

2 solutions were already suggested in the comments: 评论中已经提出了2个解决方案:

  1. Binary VLOOKUP - see here 二进制VLOOKUP-参见此处
  2. Reducing one of your VLOOKUPs 减少您的VLOOKUP

These will definitely optimize your formulas but if you want your query to run in a couple of seconds max use MS Query... 这些绝对可以优化您的公式,但是如果您希望查询在最大几秒钟内运行,请使用MS Query ...

Solution 2 (the fastest - couple sec) 解决方案2(最快-几秒)

Use this SQL in an MS Query: 在MS查询中使用以下SQL:

SELECT com.F FROM [CurrentSheet$] as curr 
LEFT JOIN [Comment_data$] as com 
ON (curr.A + curr.B) = com.A

This is how it works. 这就是它的工作方式。 Below I created two example tables. 下面,我创建了两个示例表。

Worksheet name: CurrentSheet 工作表名称: CurrentSheet

在此处输入图片说明

Worksheet name: Comment_data 工作表名称: Comment_data

在此处输入图片说明

The F column in CurrentSheet is the MS Query (appended to the original table). CurrentSheet中的F列是MS查询(附加到原始表中)。 All you need to do is refresh the Query using VBA or right-click and hit refresh. 您需要做的就是使用VBA刷新查询或右键单击并单击刷新。

How to create an MS Query in Excel? 如何在Excel中创建MS查询?

Two ways: 两种方式:

  1. Go to Data -> From Other Sources -> From Microsoft Query 转到数据 -> 从其他来源 -> 从Microsoft Query
  2. Download my SQL AddIn (free and open sources) here and just input the output range of the query (F1) and input the SQL and hit Ok 在此处下载我的SQL AddIn(免费和开放源代码),只需输入查询(F1)的输出范围,然后输入SQL并单击“ 确定”即可

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM