【问题标题】:can't assign to array -vba无法分配给数组-vba
【发布时间】:2017-01-26 05:44:44
【问题描述】:

我正在尝试实现下一个代码并得到错误 -

不能分配给数组

错误在哪里?请注意,如果我输入 Dim arrf() As Variant 而不是 Dim arrf(5) As Variant 我会收到错误 -

类型不匹配

Public Function calc(ByVal value As Integer, ByVal num As Integer) As Variant()

Dim arr(5) As Variant
Dim x As Double

If value >= num Then
    x = value - Application.RoundDown(value / num, 0) * num
    arr(0) = x
    arr(1) = num - arr(0)
    arr(2) = Application.RoundUp(value / num, 0)
    arr(3) = 1
    arr(4) = Application.RoundDown(value / num, 0)
    arr(5) = 1
Else
    x = num - Application.RoundDown(num / value, 0) * value
    arr(0) = x
    arr(1) = value - arr(0)
    arr(2) = Application.RoundUp(num / value, 0)
    arr(3) = 1
    arr(4) = Application.RoundDown(num / value, 0)
    arr(5) = 1
    calc = arr
End If

End Function


Sub cellsfunc()

With Application
    .DisplayAlerts = False
    .EnableEvents = False
    .ScreenUpdating = False
End With

Dim lastrow As Integer
Dim counter As Integer

Dim arrf(5) As Variant

lastrow = Cells(Rows.Count, 2).End(xlUp).Row
For counter = 2 To lastrow Step 2
    arrf = calc(Cells(4, counter), Cells(4, counter + 1))
Next counter

With Application
    .DisplayAlerts = True
    .EnableEvents = True
    .ScreenUpdating = True
End With

End Sub

提前感谢所有帮助者

【问题讨论】:

  • 你需要在num等于0的地方添加一个条件,否则你会在x = value - Application.RoundDown(value / num, 0) * num行中得到一个错误,因为你试图将其设置为零
  • 要查看错误在哪里,请在常规选项cpearson.com/excel/BreakInClassModule.aspx中选择Break In Class Module@

标签: arrays vba function


【解决方案1】:

您需要将aarf 声明为常规变体而不是数组。 VBA 将为您转换它。

Dim arrf As Variant

【讨论】:

    【解决方案2】:

    您的函数 calc() 存在问题:它仅在通过 else 而不是 if 时返回一个值

    应该是这样的:

    Public Function calc(ByVal value As Integer, ByVal num As Integer) As Variant()
    
      Dim arr(5) As Variant
      Dim x As Double
      If value >= num Then
        x = value - Application.RoundDown(value / num, 0) * num
        arr(0) = x
        arr(1) = num - arr(0)
        arr(2) = Application.RoundUp(value / num, 0)
        arr(3) = 1
        arr(4) = Application.RoundDown(value / num, 0)
        arr(5) = 1
      Else
         x = num - Application.RoundDown(num / value, 0) * value
        arr(0) = x
        arr(1) = value - arr(0)
        arr(2) = Application.RoundUp(num / value, 0)
        arr(3) = 1
        arr(4) = Application.RoundDown(num / value, 0)
        arr(5) = 1
      End If
      calc = arr ' <------- THIS
    End Function
    

    【讨论】:

      【解决方案3】:

      您已将 arrf 声明为 固定大小 数组:

      Dim arrf(5) As Variant
      

      数组返回函数不能返回固定大小的数组 - 只能返回动态数组。您只需要将其声明为动态数组:

      Dim arrf() As Variant
      

      【讨论】:

        【解决方案4】:

        如果您在 VBA 脚本中使用类型化数组,请始终使用 ReDim.. 大小初始化。您可以在字典值或任何地方使用类型化数组,如常规变量。

        Public Function readData(ws As Worksheet, arr As Scripting.Dictionary) As Boolean
          Dim iRow as long 
          Dim key as String 
          Dim sVal() As String
          ReDim sVal(0 to 1) as String 
        
          For iRow=2 to 1000
            key = ws.cells(iRow,1)
            sVal(0) = ws.Cells(iRow, 5)
            sVal(1) = ws.Cells(iRow, 6)
            call arr.Add(key, sVal)
          Next
          readData=true 
        End Function
        
        Public Function writeData(ws As Worksheet, arr As Scripting.Dictionary) As Long
          Dim iRow as long 
          Dim key as String 
          Dim sVal() As String
          ReDim sVal(0 to 1) as String 
        
          For iRow=2 to 1000
            key = ws.cells(iRow,1)
            If arr.Exists(key) then
              sVal = arr.Item(key)
              ws.Cells(iRow, 5) = sVal(0)
              ws.Cells(iRow, 6) = sVal(1)
            End If
          Next
          writeData=true 
        End Function
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-02-11
          • 1970-01-01
          • 1970-01-01
          • 2012-03-08
          • 2019-05-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多