将范围数据复制到阵列时键入不匹配VBA

时间:2017-08-22 07:54:01

标签: arrays excel vba excel-vba

所以我正在VBA中处理这个模块,我可以

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

  2. 计算该数组中每个整数的出现次数。

  3. 我试图将整数数组元素与整数类型进行比较,但它给出了类型不匹配错误,所以我试图找出range.offset.value的数据类型,但它总是给出类型不匹配。

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

    编辑:

    我尝试将所有数组转换为Variant,但现在If语句给出了类型不匹配错误。 如果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
    

6 个答案:

答案 0 :(得分:1)

将单元格范围转换为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

答案 1 :(得分:0)

很可能无法将值解析为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。在收到错误后,请查看即时窗口中的值。

答案 2 :(得分:0)

一些注意事项:

  1. Excel将所有数值处理为Double
  2. Range.Value始终为多个单元格返回Variant/Variant()
  3. 您无法直接在VBA中投射数组
  4. 检查以下代码的注释。

    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
    

答案 3 :(得分:0)

您的宏正在尝试将您指定的整个范围(以及偏移给定量的范围)放入每个操作的数组中。由于数组不能保持范围,因此您需要将它们更改为范围内每个单元格中保存的值:

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

This page有一些关于开始在VBA中使用数组的好信息。

答案 4 :(得分:0)

首先,您的数组与您的范围大小不同 - 您的数组有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

答案 5 :(得分:0)

当源范围恰好是一个单元格时,我遇到了这个问题。我应用了Chip Pearson的解决方案,然后解决了这个问题。

来自查尔斯·皮尔森(Charles Pearson)出色的article on Arrays and Ranges:

  

在工作表上的范围是单个单元格时,有一种特殊情况。扩展上面的代码,如果范围可能是单个单元格,则应使用下面的代码:

Dim Arr() As Variant
Dim RangeName As String
Dim R As Long
Dim C As Long
Dim RR As Range

RangeName = "TheRange"
Set RR = Range(RangeName)
If RR.Cells.Count = 1 Then
    ReDim Arr(1 To 1, 1 To 1)
    Arr(1, 1) = RR.Value
Else
    Arr = Range(RangeName)
End If

来源:www.cpearson.com/excel/ArraysAndRanges.aspx版权所有,2018,查尔斯·皮尔逊(Charles H. Pearson)