swap cells in same column based on multiple criteria

RedOctoberKnight

Board Regular
Joined
Nov 16, 2015
Messages
153
Office Version
  1. 2016
Platform
  1. Windows
What if i wanted to switch the contents of two cells that are in the same column? But have it switch with a specified cell based on a cell value.

For example,

ABCD
JimCatBobSWITCH
DanDog
BobBird
GregCat

So basically, if I put Bob in column "C" and put "SWITCH" in column "D", it would switch the contents of column B between Jim and Bob

ABCD
JimBirdBobSWITCH
DanDog
BobCat
GregCat

Hopefully that makes sense.

Thank you for all your help.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi,

Maybe something like this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Columns("C")) Is Nothing Or Not Intersect(Target, Columns("D")) Is Nothing Then
      If Cells(Target.Row, "C").Value <> "" And Cells(Target.Row, "D").Value = "SWITCH" Then
        Dim temp As Variant
        With Application
        .EnableEvents = False
        If Not IsError(.Match(Cells(Target.Row, "C").Value, Columns("A"), 0)) Then
          temp = Cells(.Match(Cells(Target.Row, "C").Value, Columns("A"), 0), "B").Value
          Cells(.Match(Cells(Target.Row, "C"), Columns("A"), 0), "B").Value = Cells(Target.Row, "B").Value
          Cells(Target.Row, "B").Value = temp
        End If
        .EnableEvents = True
        End With
      End If
  End If
End Sub
 
Last edited by a moderator:
Upvote 0
Solution
Hi,

Maybe something like this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Columns("C")) Is Nothing Or Not Intersect(Target, Columns("D")) Is Nothing Then
      If Cells(Target.Row, "C").Value <> "" And Cells(Target.Row, "D").Value = "SWITCH" Then
        Dim temp As Variant
        With Application
        .EnableEvents = False
        If Not IsError(.Match(Cells(Target.Row, "C").Value, Columns("A"), 0)) Then
          temp = Cells(.Match(Cells(Target.Row, "C").Value, Columns("A"), 0), "B").Value
          Cells(.Match(Cells(Target.Row, "C"), Columns("A"), 0), "B").Value = Cells(Target.Row, "B").Value
          Cells(Target.Row, "B").Value = temp
        End If
        .EnableEvents = True
        End With
      End If
  End If
End Sub
Perfect! Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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