[AccessD] Swap two elements in an array

Stuart McLachlan stuart at lexacorp.com.pg
Thu Aug 18 08:41:44 CDT 2022


Since VBA doesn't have a SWAP function, you have to do it with a temporary variable
Assuming it's a one dimensional array, something like:

Temp = Arr(2) : Arr(2) = Arr(4) : Arr(4) = Temp
If you define Temp as a Variant, you can use it with any type of array.

If it's a multi-dimensional array, you have to sway each dimension:
Temp = Arr(1,2) : Arr(1,2) = Arr(1,4) : Arr(1,4) = Temp
Temp = Arr(2,2) : Arr(2,2) = Arr(2,4) : Arr(2,4) = Temp
Temp = Arr(3,2) : Arr(3,2) = Arr(3,4) : Arr(3,4) = Temp
...


On 18 Aug 2022 at 8:35, Arthur Fuller wrote:

> What is the best way to swap two elements in an array? Typically they
> are adjacent but ideally I want a method that can swap any two
> elements.
> 
> -- 
> Arthur
> -- 
> AccessD mailing list
> AccessD at databaseadvisors.com
> https://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
> 




More information about the AccessD mailing list