API reference · Arrays
Exchange
Swap two Variant values in the caller through ByRef arguments.
Signature#
Function Exchange(ByRef val_a, ByRef val_b) As VariantParameters#
| Parameter | VBA type | Default | Description |
|---|---|---|---|
val_a | VariantByRef | Required | First value. The parameter has no explicit As clause, so its type is Variant. It is replaced with the original val_b. |
val_b | VariantByRef | Required | Second Variant value, replaced with the original val_a. Pass writable variables of compatible type. |
Returns#
Variant containing Empty. This function never assigns a value to its function name; the useful result is the change to both ByRef arguments.
Example#
Before
BeforeValues / state
No array dimensions
| Item | Value |
|---|---|
| debit | 125.5 |
| credit | 80 |
After
AfterValues / state
No array dimensions
| Item | Value |
|---|---|
| debit | 80 |
| credit | 125.5 |
The variables change in place. The function itself returns Empty.
Shaded headers are array indices, not data. Quoted cells are text. Values shown are derived from the source, not an Excel execution.
Sub ExampleExchange()
Dim a As New Arrays
Dim debit As Variant, credit As Variant
Dim ignored As Variant
debit = 125.5
credit = 80
ignored = a.Exchange(debit, credit)
Debug.Print debit, credit ' Expected: 80, 125.5
Debug.Print IsEmpty(ignored) ' Expected: True
End SubBehavior and edge cases#
- This is the public Arrays function that deliberately changes its input arguments. Most other functions accept vArr ByVal and return an output array.
- The implementation uses ordinary Variant assignment through a temporary variable. It performs no array-shape or type validation.
- Although declared as a Function, it has no meaningful return value. Assign the return to an ignored Variant or invoke it with Call.
- For objects requiring Set, this routine is not an object-reference swap. Arrays and other non-scalar values follow VBA assignment behavior.
Performance#
Three value assignments. For scalar values, time and auxiliary storage are O(1). Copying array-valued Variants can require work and storage proportional to their contents.
View implementation
Function Exchange(ByRef val_a, ByRef val_b) As Variant
'Swaps two values passed by reference
Dim vTemp As Variant
Dim temp As Variant
temp = val_b
val_b = val_a
val_a = temp
End Function