VBA 从用户表单更新公共数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9109533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:07:44  来源:igfitidea点击:

VBA update public array from userform

vbamultidimensional-arrayexcel-vbaparameter-passinguserform

提问by Paolo Cesari

I have a VBA Sub that create and handle an array:

我有一个创建和处理数组的 VBA Sub:

Option Base 1
Public myArr(20, 10) As Variant

Sub Stackoverflow()
Dim x, y As Integer
    'Put some values to array (only in the first 15 rows)
    For x = 1 To 15
        For y = 1 To 10
            myArr(x, y) = (x * y + 8)
        Next y
    Next x
End Sub

Then there is a userform with a commandbutton that should be able to handle the array myArr.

然后是一个带有命令按钮的用户窗体,它应该能够处理数组 myArr。

Private Sub commandbutton_Click()
Dim a, b As Integer
    'Put some other values in the remaining rows
    For a = 16 To 20
        For b = 1 To 10
            myArr(a, b) = (a * b + 3)
        Next b
    Next a
End Sub

How can I pass the array to the userform? And how the Sub can view the updated array? Thanks

如何将数组传递给用户表单?Sub 如何查看更新后的数组?谢谢

采纳答案by Tony Dallimore

Your code worked perfectly for me once I had completed it. I can only assume you do not know how to call the userform.

一旦我完成了它,你的代码对我来说非常适合。我只能假设您不知道如何调用用户表单。

I copied your Stackoverflow()to a module without changes.

我将您复制Stackoverflow()到一个模块中而没有更改。

To the same module I added:

对于我添加的同一个模块:

Sub Main()

  Dim RowMyArr As Long
  Dim ColMyArr As Long

  Load UserForm1

  Call Stackoverflow

  UserForm1.Show

  Debug.Print "    ";
  For ColMyArr = LBound(myArr, 1) To UBound(myArr, 1)
    Debug.Print Right("   " & ColMyArr, 4) & " ";
  Next
  Debug.Print

  For RowMyArr = LBound(myArr, 2) To UBound(myArr, 2)
    Debug.Print Right("  " & RowMyArr, 3) & " ";
    For ColMyArr = LBound(myArr, 1) To UBound(myArr, 1)
      Debug.Print Right("   " & myArr(ColMyArr, RowMyArr), 4) & " ";
    Next
    Debug.Print
  Next

End Sub

I created a user form and did not change the default name from UserForm1. I added a button, named it commandbuttonand copied your code to the form's code area. Before the End Sub, I added Unload UserForm1to close and exit the form.

我创建了一个用户表单并且没有更改 UserForm1 中的默认名称。我添加了一个按钮,命名它commandbutton并将您的代码复制到表单的代码区域。在 之前End Sub,我添加Unload UserForm1了关闭和退出表单。

I ran Main and the following was output to the immediate window:

我运行 Main 并且以下输出到即时窗口:

       1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20 
  1    9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   19   20   21   22   23 
  2   10   12   14   16   18   20   22   24   26   28   30   32   34   36   38   35   37   39   41   43 
  3   11   14   17   20   23   26   29   32   35   38   41   44   47   50   53   51   54   57   60   63 
  4   12   16   20   24   28   32   36   40   44   48   52   56   60   64   68   67   71   75   79   83 
  5   13   18   23   28   33   38   43   48   53   58   63   68   73   78   83   83   88   93   98  103 
  6   14   20   26   32   38   44   50   56   62   68   74   80   86   92   98   99  105  111  117  123 
  7   15   22   29   36   43   50   57   64   71   78   85   92   99  106  113  115  122  129  136  143 
  8   16   24   32   40   48   56   64   72   80   88   96  104  112  120  128  131  139  147  155  163 
  9   17   26   35   44   53   62   71   80   89   98  107  116  125  134  143  147  156  165  174  183 
 10   18   28   38   48   58   68   78   88   98  108  118  128  138  148  158  163  173  183  193  203 

I have not checked the values but I assume they are correct.

我没有检查过这些值,但我认为它们是正确的。

回答by Romeo

Its a public array, accessible from any form in your application . . . why do you need to pass it anyway? This one describes how to pass parameter to forms in access vba.

它是一个公共数组,可从您的应用程序中的任何形式访问。. . 为什么你需要通过它呢?本篇介绍了如何在access vba中将参数传递给表单。

Passing parameters between forms in MS Access

在 MS Access 中的表单之间传递参数