将范围数据复制到数组时,types不匹配VBA

所以我正在VBA上使用这个模块

  1. 将数据从工作表范围复制到数组。

  2. 统计该数组中每个整数的发生次数。

我试图比较整数数组元素与整数types,但它给了一个types不匹配的错误,所以我试图找出range.offset.value的数据types,但它总是给types不匹配。

这是我的代码。 请帮忙!!!

编辑:

我试图将所有的数组转换为Variant,但是现在If语句给了我一个types不匹配的错误。 如果k = first(j)那么total(k)= total(k)+ 3

'------------------------- Option Explicit Sub Task1() Dim total(32) As Integer Dim first(32) As Integer Dim second(32) As Integer Dim third(32) As Integer Dim firs As Range Dim secon As Range Dim thir As Range Set firs = Range("B2:B33") Set secon = Range("C2:C33") Set thir = Range("D2:D33") Dim i As Integer 'copying data from first , second and third range to specific arrays 'gives type mismatchy error here For i = 0 To 32 first(i) = firs.Offset(i, 0).Value second(i) = secon.Offset(i, 0).Value third(i) = thir.Offset(i, 0).Value Next 'initialize total array with 0 For i = 0 To 32 total(i) = 0 Next Call reader(total, first) End Sub '--------------------------------------------------- Sub reader(total() As Integer, first() As Integer) Dim i, j, k As Integer 'Checks the occurance of every array element For i = 0 To 32 'gives type mismatch error here k = first(i) j = i + 1 For j = i To 32 If k = first(j) Then total(k) = total(k) + 3 Next Next End Sub 

将单元格范围转换为1d数组可能会有所帮助

  Dim aaa As Variant aaa = Range("B2:B33") ' 2D array (32x1) Debug.Print aaa(2, 1) aaa = Application.Transpose(aaa) ' 1D array Debug.Print aaa(2) ' note: if you start with row data, then do a second transpose to get 1D array 

很有可能这个值不能被parsing为Integer 。 只需将以下代码添加到代码中,并查看发生错误的值:

 For i = 0 To 32 Debug.Print firs.Offset(i, 0) Debug.Print secon.Offset(i, 0) Debug.Print thir.Offset(i, 0) first(i) = firs.Offset(i, 0) second(i) = secon.Offset(i, 0) third(i) = thir.Offset(i, 0) Next 

可能的修正 – 只是猜测你可以在你的代码中将Integer改成LongDouble 。 或者在获得错误之后,在即时窗口中查看值。

一些说明:

  1. Excel将所有数值作为Double处理
  2. Range.Value总是返回多个单元格的Variant/Variant()
  3. 您不能直接在VBA中投射数组

检查下面的代码注释。

 Option Explicit Sub Task1() Dim total(32) As Integer 'Range.Value return Variant() for multiple cells. VBA doesn't cast arrays! Dim first, second, third Dim firs As Range Dim secon As Range Dim thir As Range Set firs = Range("B2:B33") Set secon = Range("C2:C33") Set thir = Range("D2:D33") 'Assuming you are copying data to array for performance reasons, make just a single assignement: first = firs.Value second = secon.Value third = thir.Value 'Variables in VBA are always initialized, so all integers already 0% 'For i = 0 To 32 ' total(i) = 0 'Next Call reader(total, first) End Sub Sub reader(total() As Integer, first) 'This notation from VB.Net doesn't work in VBA: i and j are Variant! 'Dim i, j, k As Integer 'Don't undersatnd your code objective, propose new counter: Dim v For Each v In first 'Excel only handles Double If VarType(v) = vbDouble Then total(v) = total(v) + 1 Next End Sub 

你的macros试图把你指定的整个范围(当然,范围偏移一个给定的量)放到每个操作的数组中。 由于该数组不是保存范围,所以需要将它们更改为在每个单元格中保存的值,范围为:

 For i = 0 To 32 first(i) = firs.Cells(i, 1).Value second(i) = secon.Cells(i, 1).Value third(i) = thir.Cells(i, 1).Value Next 

甚至更简单:

 Dim first() As Variant ' declare an unallocated array Arr = Range("B2:B33")' Arr is now an allocated array 

这个页面有一些关于在VBA中使用数组的入门信息。

首先,你的数组的大小与你的范围不一样 – 你的数组有33个元素,范围只有32个。其次,你试图给数组的每个元素指定多个单元格的范围。 你的循环应该是这样的:

 For i = 0 To 31 first(i) = firs.Cells(i + 1, 1).Value second(i) = secon.Cells(i + 1, 1).Value third(i) = thir.Offset(i + 1, 1).Value Next 

请注意使用Cells而不是Offset 。 (虽然你也可以OffsetResize