I am trying to make change to the array named arraySrc in the following manner:
Dim arraySrc(0 To 1) As Integer
arraySrc(0) = 1
arraySrc(1) = 2
Dim arrayTmp
arrayTmp = arraySrc
arrayTmp(0) = 0
arrayTmp(1) = 1
Actually, I want to use one name as a handle to make change to multiple arrays individually by case, for example, I have a function to return the array name, I want to then set the returned array name to arrayTmp, then make change to arrayTmp directly using the format arrayTmp(0)=0 eg, hoping to make change to the original array
However, by using variant doesn't work. Can anybody please let me know how to implement this?
If you want to change values in arraySrc
you need to refer to the indices of that array.
eg which you have already done.
arraySrc(0) = 1
arraySrc(1) = 2
Just because you copy arraySrc
to arrayTmp
, the latter is not going to keep the reference to arraySrc
.
However this is possible if you had passed a reference of arraySrc
via a funtion's parameter.
eg
Option Explicit
Sub myArrays()
Dim arraySrc(0 To 1) As Integer
arraySrc(0) = 1
arraySrc(1) = 2
'-- the referencing
arrayReference arraySrc
End Sub
Function arrayReference(ByRef varr() As Integer) As Variant
If Not IsVarArrayEmpty(varr) Then
varr(0) = 0
varr(1) = 1
End If
arrayReference = varr
End Function
'--check for empty array - additional
Function IsVarArrayEmpty(anArray As Variant) As Boolean
Dim i As Integer
On Error Resume Next
i = UBound(anArray, 1)
If Err.Number = 0 Then
IsVarArrayEmpty = False
Else
IsVarArrayEmpty = True
End If
End Function
Do you mean something like this? The ByRef argument means that the source array, passed as a parameter will also be changed:
Sub test()
Dim arraySrc(0 To 1) As Integer
arraySrc(0) = 1
arraySrc(1) = 2
PassByRef arraySrc
Debug.Print arraySrc(0)
Debug.Print arraySrc(1)
End Sub
Sub PassByRef(ByRef arrayTmp() As Integer)
arrayTmp(0) = 0
arrayTmp(1) = 1
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.