SwapNumbers

Swap two numbers variables without using third variable
A trick worth saving originally from https://www.vbausefulcodes.com/vbausefulcodes/39/swap-values-between-two-variables-without-using-third-variable-in-vba

CodeFunctionName
What is this?

Public

Tested

Original Work
Sub SwapNumbers(ByRef XVal, ByRef YVal)
    ' Swap values between two numbers without using third variable in VBA
    ' A Popular Question asked in most of the VBA Interviews.
    ' A simple addition and subtraction logic is used to swap values between two numbers.
    ' https://www.vbausefulcodes.com/vbausefulcodes/39/swap-values-between-two-variables-without-using-third-variable-in-vba
    ' Ready to use VBA Useful Codes! Try Now! http://play.google.com/store/apps/details?id=com.vbausefulcodes.dp https://www.vbausefulcodes.com
    ' XVal = 10        | YVal = 5
    ' MsgBox "XVal : " & XVal & ", YVal : " & YVal & vbCrLf
    ' Code to swap 'XVal' and 'YVal'
    XVal = XVal + YVal '// XVal now becomes 15
    YVal = XVal - YVal '// YVal becomes 10= old XVal
    XVal = XVal - YVal '// XVal becomes 5 = old YVal
    ' MsgBox "XVal : " & XVal & " YVal : " & YVal & vbCrLf
End Sub

ByRef XVal, ByRef YVal

Views 121

Downloads 69

CodeID
DB ID

ANmarAmdeen
618
Attachments
Revisions

v1.0

Monday
October
17
2022