VBA Word 如何将数组的一部分传递给子程序?

问题描述

我想将数组的一部分传递给子程序!

Sub main()
    Dim i(10) As Byte
    Dim j As Integer

    For j = LBound(i) To UBound(i)
        i(j) = j
    Next
    
    Call subr(i())

End Sub
Sub subr(i() As Byte)
    Dim j As Integer

    For j = LBound(i) To UBound(i)
        Debug.Print  i(j)
    Next
    
End Sub

本示例将整个数组传递给 subr 并打印出来。

我想要这样的东西(不起作用):

Call subr(i(L_index to U_index))

L_indexU_index 确定要通过的范围。

解决方法

唯一的方法是复制你感兴趣的数组的跨度 in,而是传递数组和范围:

Call subr(i(),3,5)

Sub subr(arr() As Byte,startIndex As Long,endIndex As Long)
   
   For i = startIndex To endIndex
        Debug.Print arr(i)
   Next
    
End Sub

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...