数组vba - 类型不匹配:数组或用户定义的类型

时间:2015-08-19 14:49:19

标签: vba excel-vba excel

我遇到了将数组传递给sub(接收类型不匹配:数组或用户定义的类型)的问题。我使用其他语言的数组结构,我必须在vba中遗漏一些东西,因为我无法使用它。我已在下面发布了代码段以供评论:

Private Sub x()
    Dim ssn_vals(1 To 9) As String
    Dim ssn_cells(1 To 9) As String

    'define employee object
     Dim emp As Employee

     'create new employee foreach row
      Set emp = New Employee

    'ssn values
        ssn_vals(1) = cell.Offset(0, 60)
        ssn_vals(2) = cell.Offset(0, 61)
        ssn_vals(3) = cell.Offset(0, 62)
        ssn_vals(4) = cell.Offset(0, 63)
        ssn_vals(5) = cell.Offset(0, 64)
        ssn_vals(6) = cell.Offset(0, 65)
        ssn_vals(7) = cell.Offset(0, 66)
        ssn_vals(8) = cell.Offset(0, 67)
        ssn_vals(9) = cell.Offset(0, 68)



        'ssn cell addresses
        ssn_cells(1) = cell.Offset(0, 60).Address
        ssn_cells(2) = cell.Offset(0, 61).Address
        ssn_cells(3) = cell.Offset(0, 62).Address
        ssn_cells(4) = cell.Offset(0, 63).Address
        ssn_cells(5) = cell.Offset(0, 64).Address
        ssn_cells(6) = cell.Offset(0, 65).Address
        ssn_cells(7) = cell.Offset(0, 66).Address
        ssn_cells(8) = cell.Offset(0, 67).Address
        ssn_cells(9) = cell.Offset(0, 68).Address

        emp.setSSN = ssn_vals
        emp.setSSN_cells = ssn_cells

        'validate ssn
        emp.validateSSN (ssn_vals)

end sub

我在员工类内部有一个子定义

'validate SSN
Public Sub validateSSN(ssn() As Variant)
    Dim i As Integer
    'Dim orig_array(1 To 9) As Variant
    Dim multipler_array(1 To 9) As Integer
    Dim summation_array(1 To 9) As Integer


    multipler_array(1) = 1
    multipler_array(2) = 2
    multipler_array(3) = 1
    multipler_array(4) = 2
    multipler_array(5) = 1
    multipler_array(6) = 2
    multipler_array(7) = 1
    multipler_array(8) = 2
    multipler_array(9) = 1



    If Me.getSinOrT = "sin" Then
        For i = LBound(ssn(i)) To UBound(ssn(i))
           summation_array(i) = (Int(ssn(i)) * Int(multipler_array(i)))
           Debug.Print summation_array(i)
    Next i

    Else


     End If

 End Sub

任何关于如何解决这个微不足道的事情的建议将不胜感激。 提前致谢, 迈克尔

2 个答案:

答案 0 :(得分:0)

您使用单元格作为范围,但没有 Dim 对其进行操作或设置它。可能还有其他问题

答案 1 :(得分:0)

感谢您提供有用的评论 - 这让我意识到我没有必要再次将ssn数组传递给员工对象,因为它的数据已经保存在Employee中。为了纠正问题,我在Employee中创建了一个基于员工现有SIN验证SIN的方法。非常感谢......