Home Excel Powerpoint Word Outlook Error Codes

VBA Useful Codes

Welcome to the World of VBA
Home > VBAUsefulCodes

Swap values between two variables 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 variables.

Sub swapvalues()
x = 10
y = 5

MsgBox "X : " & x & " Y : " & y & vbCrLf

'Code to swap 'x' and 'y'
x = x + y '// x now becomes 15
y = x - y '// y becomes 10
x = x - y '// x becomes 5

MsgBox "X : " & x & " Y : " & y & vbCrLf

End Sub

Subscribe

Enter your e-mail below and get notified on the latest blog posts.




Tags

VBA Outlook KPI Excel Alt+F11 Dashboards Macros Recording Graphs Automation Developer WaterFall Powerpoint Charts Pivot Tables Forecast Charts


Follow Us