【问题标题】:Passing 2-dimensional arrays between subroutines Excel VBA在子例程Excel VBA之间传递二维数组
【发布时间】:2018-07-29 09:10:12
【问题描述】:

我似乎无法让它工作。

我想将一个二维数组从一个子程序传递给其他应该进行数据操作的子程序。

我从 Sheet1 上的命令按钮调用下面的子程序。在 Module1 中,我已将变量声明为公开的。在子例程 ReadData 中,在 End If 语句中,InputArray 填充了一个数组,该数组由用户在输入框后面标记的数字组成。子程序 ReadData 完成后,InputArray 为空。

我做错了什么明显且明显愚蠢的事情?

** Sheet1**:
Private Sub CommandButton1_Click()
    Call ReadData
End Sub

**Module1**:
Option Explicit
Option Base 1
Public MyRange, InputArray As Variant

Sub ReadData()
    Set MyRange = Application.InputBox("Mark the data array", Type:=8)
    If Not MyRange Is Nothing Then
        InputArray = MyRange
    End If
End Sub 

【问题讨论】:

  • 因为 myrange 是一个对象,您需要“设置”它,或者如果您只想要值,请使用“myrange.values”

标签: excel vba subroutine


【解决方案1】:

如下填充数组并在本地窗口中检查。通过使用 .Value,您可以从选定的工作表范围创建一个二维数组。 MyRange 我认为可以是本地范围并声明为 Range。 InputArray 也应该是一个本地范围,并且简单地作为参数传递给其他子/函数。

Public InputArray As Variant

Sub ReadData()
    Dim MyRange As Range
    Set MyRange = Application.InputBox("Mark the data array", Type:=8)
    If Not MyRange Is Nothing Then
        InputArray = MyRange.Value
        Stop '<==Delete me after inspection
    End If
End Sub

【讨论】:

    【解决方案2】:

    根本不需要公共变量。

    option explicit
    
    '** Sheet1**:
    Private Sub CommandButton1_Click()
        dim InputArray as variant, i as long, j as long
    
        ReadData InputArray 
    
        for i = lbound(InputArray, 1) to ubound(InputArray, 1)
            for j = lbound(InputArray, 2) to ubound(InputArray, 2)
                debug.print InputArray(i, j)
            next j
        next i
    
    End Sub
    
    '**Module1**:
    Option Explicit
    
    Sub ReadData(byref arr as variant)
        dim myrange as range
    
        Set MyRange = Application.InputBox("Mark the data array", Type:=8)
    
        If Not MyRange Is Nothing Then
            arr  = MyRange.value
        End If
    
    End Sub 
    

     1 
     4 
     7 
     10 
     2 
     5 
     8 
     11 
     3 
     6 
     9 
     12 
    

    【讨论】:

      【解决方案3】:

      按照@Jeeped 的方法,您可以通过将ReadData() 转换为Function 来简化代码,如下所示:

      表 1:

      Function ReadData() As Variant
          On Error Resume Next
          ReadData = Application.InputBox("Mark the data array", Type:=8).Value
      End Function
      

      模块1:

      Option Explicit
      Private Sub CommandButton1_Click()
          Dim InputArray As Variant
      
          InputArray = ReadData
      
          If IsEmpty(InputArray) Then
              MsgBox "not a valid selection", vbCritical
          Else
              'do your stuff with InputArray
          End If
      End Sub
      

      或者你可以让你的ReadData() 函数返回一个Boolean 来标记一个成功的范围选择:

      表 1:

      Function ReadData(arr As Variant) As Boolean
          On Error Resume Next
          arr = Application.InputBox("Mark the data array", Type:=8).Value
          ReadData = Not IsEmpty(arr)
      End Function
      

      模块1:

      Option Explicit
      
      Private Sub CommandButton1_Click()
          Dim InputArray As Variant
      
          If ReadData(InputArray) Then
              'do your stuff with InputArray
          Else
              MsgBox "not a valid selection", vbCritical
          End If        
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-20
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        • 2012-05-14
        • 2014-03-20
        相关资源
        最近更新 更多