我要根据每行中的数据创建一个串联的键,然后将其添加到字典以及该行的结尾列中。
由于某种原因,当仅将其添加到列中或仅将其添加到字典中时,它都可以工作,但是当我同时执行这两项操作时,它将永远需要运行。
难道不应该在列末尾添加一个值(下面的粗体)会增加时间?Dim unique_key
Dim sht
Set sht = ThisWorkbook.Worksheets("data")
Dim colNum
colNum = sht.Cells(2, sht.Columns.Count).End(xlToLeft).Column
For i = 2 To Rows.Count
unique_key = Concatenate(CLng(i), id_cols)
**Cells(CLng(i), colNum).Value = unique_key**
If dict.Exists(unique_key) Then
GoTo continue
Else
dict.Add unique_key, Cells(i, 10)
End If
continue:
Next i
答案 0 :(得分:0)
将尝试更新您的代码:
Option Explicit
Public id_cols 'so you can use it between sub routines
'not sure if the below is part of a function or sub, so not listing
Dim unique_key as string, sht as worksheet, i as long, colNum as long, dict as scripting.dictionary
set dict = NEW scripting.dictionary
Set sht = ThisWorkbook.Worksheets("data")
with sht
colNum = .Cells(2, .Columns.Count).End(xlToLeft).Column
For i = 2 To .Rows.Count
unique_key = i & id_cols
.Cells(i, colNum).Value = unique_key
If NOT dict.Exists(unique_key) Then dict(unique_key) = .Cells(i, 10)
Next i
耦合事物...
您想将值放在最后一列(colNum)还是最后一列+1?
创建字典后您要如何处理?
请确保对您的引用进行限定...这将是我在您的代码中只能看到的唯一打
除此之外,根据id_cols
中的内容,您可能对密钥的理解方式有疑问。
编辑1:
意识到我忘记添加我之前建议的内容了……&
而不是Concatenate
。已更新。