swap values in vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I wrote the code below but I want to improve it using array if possible. I want my code not only swap one cell with another cell but group of cells with another group. I want to make it dynamic means number of cells can be change but it is always cells in column B with cells in column A. Is that possible? Thank you very much.

Code:
Sub myswap()
    Dim x As Variant
    x = Range("a1").Value
    Range("a1").Value = Range("b1")
    Range("b1").Value = x
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Well what would you like to happen if the ranges differ in size?
 
Upvote 0
You are already using an array and you could easily expand on the posted code, if the ranges to swap are the same size.
VBA Code:
Sub myswap()
Dim x As Variant

    x = Range("A1:A10").Value
    Range("A1:A10").Value = Range("B1:B10").Value
    Range("B1:B10").Value = x
    
End Sub
 
Upvote 0
Thanks for your reply. If B has lets say 10 cells and A has 5 cells then after the swap column A will have 10 cells and column B will have 5 cells. Thanks once again
 
Upvote 0
You are already using an array and you could easily expand on the posted code, if the ranges to swap are the same size.
VBA Code:
Sub myswap()
Dim x As Variant

    x = Range("A1:A10").Value
    Range("A1:A10").Value = Range("B1:B10").Value
    Range("B1:B10").Value = x
   
End Sub
Thank you. Both column A and B ended empty cells from 1:10. Thank you.
 
Upvote 0
Something like this?
VBA Code:
Public Sub mySwap(source, target As Range)
  Dim cell, temp, orgSht As Variant
  Application.EnableEvents = False
  Set orgSht = ActiveSheet
  ActiveWorkbook.Sheets.Add.Name = "swapTemp"
  Set temp = ActiveWorkbook.Sheets("swapTemp").Range(source.Address)
  temp.Value = orgSht.Range(source.Address).Value
  For Each cell In target.Cells
    source.Cells(cell.Row - target.Offset(0, 0).Row + 1, cell.Column - target.Offset(0, 0).Column + 1).Value = cell.Value
  Next
  For Each cell In temp.Cells
    target.Cells(cell.Row - temp.Offset(0, 0).Row + 1, cell.Column - temp.Offset(0, 0).Column + 1).Value = cell.Value
  Next
  Set cell = Nothing
  Set temp = Nothing
  Application.DisplayAlerts = False
  ActiveWorkbook.Sheets("swapTemp").Delete
  Application.DisplayAlerts = True
  Application.EnableEvents = True
  orgSht.Activate
  Set orgSht = Nothing
End Sub
 
Upvote 0
What about:
VBA Code:
[A:A].Cut
[C:C].Insert
 
Upvote 0
Maybe this:
VBA Code:
Sub try288()
Dim n As Long
Dim va

n = Range("A:B").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
va = Range("A1:A" & n)
Range("A1:A" & n).Value = Range("B1:B" & n).Value
Range("B1:B" & n) = va

End Sub
 
Upvote 0
Pretty sure the rest of these examples leave cells with #N/A when cell ranges differ.
 
Upvote 0

Forum statistics

Threads
1,215,137
Messages
6,123,252
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top