+ -
当前位置:首页 → 问答吧 → 频数统计的时候提示无效的Next控件变量引用

频数统计的时候提示无效的Next控件变量引用

时间:2011-08-08

来源:互联网

各位好,我在写对A列数据进行频数统计的代码编写的时候,运行时遇到了提示“无效的Next控件变量引用”,不知道我的代码错在哪儿,不好意思,我是初学者,大家能不能帮我改下,谢谢了啊
代码如下:

Sub 频数统计()
Dim Arr '数据源
Dim Count() As Long
With ActiveSheet
Arr = Application.Transpose(.Range("a2:a" & .[a65536].End(xlUp).Row))

For i = 1 To UBound(Arr)
Count(i) = 0
For k = 1 To UBound(Arr)
If Arr(i) = Arr(k) Then
Count(i) = Count(i) + 1
k = k + 1
Else
Count(i) = Count(i)
k = k + 1
End If
Next i

On Error Resume Next
With ActiveSheet
.[b1] = "数据"
.[c1] = "数据B列中每个数出现的次数"
.[b2].Resize(UBound(Arr), 1) = Application.Transpose(Arr)
.[c2].Resize(UBound(Count), 1) = Application.Transpose(Count)
End With
On Error GoTo 0
Erase Arr, Count()
End Sub

作者: qiuxianyou   发布时间: 2011-08-08

两个for对两个next,少一个next

作者: zhz3230   发布时间: 2011-08-08

刚才这个NEXT的问题我好像解决了,有两个FOR.所以也必须有两个next
我还掉了一个END WITH,但是把这些问题改完后新问题又出现了,提示“下标越界”,怎么回事啊。代码如下:
Sub 统计()
Dim Arr '数据源
Dim Count() As Long
With ActiveSheet
Arr = Application.Transpose(.Range("a2:a" & .[a65536].End(xlUp).Row))
End With
For i = 1 To UBound(Arr)
Count(i) = 0
For k = 1 To UBound(Arr)
If Arr(i) = Arr(k) Then
Count(i) = Count(i) + 1
End If
Next k
Next i
With ActiveSheet
.[b1] = "数据"
.[c1] = "数据B列中每个数出现的次数"
.[b2].Resize(UBound(Arr), 1) = Application.Transpose(Arr)
.[c2].Resize(UBound(Count), 1) = Application.Transpose(Count)
End With
On Error GoTo 0
Erase Arr, Count()
End Sub

作者: qiuxianyou   发布时间: 2011-08-08

相关阅读 更多