永发信息网

VBA操作两个不同excel之间进行匹配查找,数据1W条左右,用双循坏匹配查找大概需要10分钟左右

答案:1  悬赏:10  手机版
解决时间 2021-02-08 05:51
  • 提问者网友:放下
  • 2021-02-07 22:37
双循坏时间操作时间太久了,怎么将双循坏程序改成用VBA字典的方式呢?
Sub SearchGroup()

'match group name
Dim fp As String, acct As String, acctb As Workbook, acctsht As Worksheet

'Application.ScreenUpdating = False

'On Error GoTo VeryEnd

'Set acct = ThisWorkbook.Path & "\Accounts 123.xlsx"
fp = ThisWorkbook.Path & "\Accounts_123.xlsm"

Set acctb = GetObject(fp)
'Set acctb = Workbooks.Open(fp)

Set acctsht = acctb.Sheets(1)

'acctsht.Cells(3, 3).Copy ThisWorkbook.Sheets(1).Cells(2, "T")

'account list row numbers
Dim Acc As Long, Ccc As Long

'account rows
Acc = acctsht.Range("A1").End(xlDown).Row
ThisWorkbook.Sheets(1).Cells(3, "T") = Acc
'closed trades report rows
Ccc = Sheet1.Range("A1").End(xlDown).Row
ThisWorkbook.Sheets(1).Cells(4, "T") = Ccc

For m = 2 To Ccc

For n = 2 To Acc

If (Sheet1.Cells(m, 2).Value = acctsht.Cells(n, 1).Value) Then

Sheet1.Cells(m, "Q") = acctsht.Cells(n, "C")

End If

Next n

Next m

'VeryEnd:
Application.ScreenUpdating = True

End Sub
最佳答案
  • 五星知识达人网友:孤老序
  • 2021-02-07 23:28
For m = 2 To Ccc

For n = 2 To Acc

If (Sheet1.Cells(m, 2).Value = acctsht.Cells(n, 1).Value) Then

Sheet1.Cells(m, "Q") = acctsht.Cells(n, "C")

End If
这两个for很浪费,即使出现多次(Sheet1.Cells(m, 2).Value = acctsht.Cells(n, 1).Value)成立, Sheet1.Cells(m, "Q") 也只能保存最后一个acctsht.Cells(n, "C")
考虑用sql查询的方式
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯