在Excel中如果testing通过值手动键入,但从单元格失败

使用excel VBA,我build立了一个字典并填充它。 我做了testing,做得很好。 之后,在一个循环中,我不得不testing一个键是否存在做一些操作:

For i3 = 2 To n 'n is the the number of rows If dicos.Exists(ActiveSheet.Range("T" & i3)) Then ActiveSheet.Range("N" & i3) = dicos(ActiveSheet.Range("T" & i3)) + 1 Else ActiveSheet.Range("N" & i3) = 1 End If Next 

它不工作,我在所有列N有1。我试图手动testing列T中的一些值,它发现它! 任何人都可以向我解释我做错了什么,为什么testing是正面的,当我手动键入值,而当程序必须从一个单元格(T列)拿到它是否定的? 非常感谢你。

 Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("Sheet1") Dim dicos As Dictionary Set dicos = new Dictionary 'Populate dicos.... For i = 2 To n 'n is the the number of rows If dicos.Exists(ws.Cells(i, 20).Value) Then ws.Cells(i, 14) = dicos.Item(ws.Cells(i, 20).Value) + 1 Else ws.Cells(i, 14) = 1 End If Next 

我相信问题是你没有使用.Value在添加剂行,因此它只是把1放在那里。 当我testing你的代码张贴时,我把+1改成了+4,在cells(i,14)而不是1中得到了4。