vba 字典当我转置字典项目时,我得到全零
vba dictionary when I transpose the dictionary Items, I get all zeros
我做的第一件事是创建一个脚本字典,设置列中每一行的键和数字零作为项目。在后续代码中,当条件满足时,我将Key的Item作为整数拉出,将其加1并将字典中的0替换为1。
为了测试代码,当我专门调用 Key 时,我按预期获得了 Item 的 1。当我将该字典的项目转置到一列中时,它给我所有的零。当我将它设置为遍历 For 循环,将 Items 放入列中的后续行时,我仍然得到全零。
我不担心人数上限,我正在检查的招聘人员只有大约 40 名。
已编辑:
我睡着了,以为我会放入更多测试代码。我在创建字典后立即进行了字典计数,结果是 41。代码执行完后,我又进行了一次计数,发现它达到了 42。
是什么让你在字典中有两个相同的键?
想法?
LRR 和 LR??最后一行变量供参考
'This creates the dictionary. Recruiter Key with a 0 item
Set RecGoalCount = CreateObject("scripting.dictionary")
For Each RecDesk In Sheets("Recruiters").Range("B2:B" & LRR)
RecGoalCount.Add RecDesk, 0
Next RecDesk
''''Here is the code that will change the dictionary item to a 1. The way _
''''it processes the sheet right now, there is only one instance where all _
''''are true which is why only 1 item gets changed from a 0 to a 1.
RecMatch = 0
For Each job In Sheets("Sheet2").Range("A2:A" & LRS2)
For Each job2 In Sheets("Sheet1").Range("D2:X" & LRS1)
OneOfInQW = WorksheetFunction.CountIf(Sheets("Sheet1").Range("D2:X" & LRS1), job)
CurrentCol = job2.Column
TrgtCol = CurrentCol - 4
TrgtCell = job2.Offset(0, -TrgtCol).Value2
RefCell = job.Offset(0, 1).Value2
If job = job2 And OneOfInQW = 1 And RefCell >= TrgtCell Then
LRS3 = Sheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
TrgtRec = CurrentCol - 1
TrgtCol = CurrentCol - 2
TrgtCell = job2.Offset(0, -TrgtCol).Value2
TrgtKey = job2.Offset(0, -TrgtRec).Value2
Sheets("Sheet3").Range("A" & (LRS3 + 1)).Value = TrgtCell
RecMatch = Int(RecGoalCount(TrgtKey))
RecMatch = RecMatch + 1
RecGoalCount.Item(TrgtKey) = Str(RecMatch)
job2.EntireRow.ClearContents
Sheets("Sheet3").Range("B" & (LRS3 + 1)).Value = job
Sheets("Sheet3").Range("C" & (LRS3 + 1)).Value = job.Offset(0, 1)
job.Clear
job.Interior.ColorIndex = 4
Sheets("Sheet3").Range("A" & (LRS3 + 1)).Interior.ColorIndex = 4
Exit For
End If
Next job2
Next job
Sheets("Recruiters").Range("E2:E" & LRR) = Application.Transpose(RecGoalCount.Items)
'This is where I get all 0s despite a condition in the code changing exactly 1 Item to a 1
Sheets("Recruiters").Range("F2:F" & LRR) = Application.Transpose(RecGoalCount.Keys)
'This is just proving to me that it captured the correct keys with the correct names, check.
Sheets("Recruiters").Range("H2") = RecGoalCount("14CBBPG")
'This proves to me that the Item became a 1
Recs = RecGoalCount.Count
'This proves to me that after the code that changes the item, the total _
count of the dictionary didn't change either.
'This code iterates through the dictionary keys to give the items, and it _
gives me all 0s despite one of the items being turned into a 1
x = 2
For Each Rec In RecGoalCount
Sheets("Recruiters").Range("E" & x) = RecGoalCount(Rec)
x = x + 1
Next Rec
一旦我能够确定我是在添加一个新键而不是更新一个键,我只需要 google 字典怎么会有重复的键。这就是我创建字典的方式。我将键添加为单元格而不是值。
感谢您的考虑。
我做的第一件事是创建一个脚本字典,设置列中每一行的键和数字零作为项目。在后续代码中,当条件满足时,我将Key的Item作为整数拉出,将其加1并将字典中的0替换为1。
为了测试代码,当我专门调用 Key 时,我按预期获得了 Item 的 1。当我将该字典的项目转置到一列中时,它给我所有的零。当我将它设置为遍历 For 循环,将 Items 放入列中的后续行时,我仍然得到全零。
我不担心人数上限,我正在检查的招聘人员只有大约 40 名。
已编辑: 我睡着了,以为我会放入更多测试代码。我在创建字典后立即进行了字典计数,结果是 41。代码执行完后,我又进行了一次计数,发现它达到了 42。
是什么让你在字典中有两个相同的键?
想法?
LRR 和 LR??最后一行变量供参考
'This creates the dictionary. Recruiter Key with a 0 item
Set RecGoalCount = CreateObject("scripting.dictionary")
For Each RecDesk In Sheets("Recruiters").Range("B2:B" & LRR)
RecGoalCount.Add RecDesk, 0
Next RecDesk
''''Here is the code that will change the dictionary item to a 1. The way _
''''it processes the sheet right now, there is only one instance where all _
''''are true which is why only 1 item gets changed from a 0 to a 1.
RecMatch = 0
For Each job In Sheets("Sheet2").Range("A2:A" & LRS2)
For Each job2 In Sheets("Sheet1").Range("D2:X" & LRS1)
OneOfInQW = WorksheetFunction.CountIf(Sheets("Sheet1").Range("D2:X" & LRS1), job)
CurrentCol = job2.Column
TrgtCol = CurrentCol - 4
TrgtCell = job2.Offset(0, -TrgtCol).Value2
RefCell = job.Offset(0, 1).Value2
If job = job2 And OneOfInQW = 1 And RefCell >= TrgtCell Then
LRS3 = Sheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
TrgtRec = CurrentCol - 1
TrgtCol = CurrentCol - 2
TrgtCell = job2.Offset(0, -TrgtCol).Value2
TrgtKey = job2.Offset(0, -TrgtRec).Value2
Sheets("Sheet3").Range("A" & (LRS3 + 1)).Value = TrgtCell
RecMatch = Int(RecGoalCount(TrgtKey))
RecMatch = RecMatch + 1
RecGoalCount.Item(TrgtKey) = Str(RecMatch)
job2.EntireRow.ClearContents
Sheets("Sheet3").Range("B" & (LRS3 + 1)).Value = job
Sheets("Sheet3").Range("C" & (LRS3 + 1)).Value = job.Offset(0, 1)
job.Clear
job.Interior.ColorIndex = 4
Sheets("Sheet3").Range("A" & (LRS3 + 1)).Interior.ColorIndex = 4
Exit For
End If
Next job2
Next job
Sheets("Recruiters").Range("E2:E" & LRR) = Application.Transpose(RecGoalCount.Items)
'This is where I get all 0s despite a condition in the code changing exactly 1 Item to a 1
Sheets("Recruiters").Range("F2:F" & LRR) = Application.Transpose(RecGoalCount.Keys)
'This is just proving to me that it captured the correct keys with the correct names, check.
Sheets("Recruiters").Range("H2") = RecGoalCount("14CBBPG")
'This proves to me that the Item became a 1
Recs = RecGoalCount.Count
'This proves to me that after the code that changes the item, the total _
count of the dictionary didn't change either.
'This code iterates through the dictionary keys to give the items, and it _
gives me all 0s despite one of the items being turned into a 1
x = 2
For Each Rec In RecGoalCount
Sheets("Recruiters").Range("E" & x) = RecGoalCount(Rec)
x = x + 1
Next Rec
一旦我能够确定我是在添加一个新键而不是更新一个键,我只需要 google 字典怎么会有重复的键。这就是我创建字典的方式。我将键添加为单元格而不是值。
感谢您的考虑。