Swap Cells Based On Content

Nick van Staden

New Member
Joined
Dec 6, 2017
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Looking for an excel VBA that will assist with the following:

if column A has content swap it with content in column B
[TABLE="width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]
FPE100
<strike></strike>
[/TD]
[/TR]
[TR]
[TD]<strike></strike>
[/TD]
[TD]
FPE101
<strike></strike>
[/TD]
[/TR]
[TR]
[TD]
FPA001
<strike></strike>
[/TD]
[TD]FPO00111[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]
FPE102
<strike></strike>
[/TD]
[/TR]
[TR]
[TD]<strike></strike>
[/TD]
[TD]FPE103[/TD]
[/TR]
</tbody>[/TABLE]
<strike></strike>
So the response should be:
[TABLE="width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]
FPE100
[/TD]
[/TR]
[TR]
[TD]<strike></strike><strike></strike>[/TD]
[TD]
FPE101
[/TD]
[/TR]
[TR]
[TD]
FPO00111
<strike></strike>[/TD]
[TD]
FPA001
[/TD]
[/TR]
[TR]
[TD]<strike></strike><strike></strike>[/TD]
[TD]
FPE102
[/TD]
[/TR]
[TR]
[TD]<strike></strike><strike></strike>[/TD]
[TD]
FPE103
<strike></strike>[/TD]
[/TR]
</tbody>[/TABLE]
<strike></strike><strike></strike>
 
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
can col B be empty and col A have a value? Can you try the below?

Code:
Sub swap()
Dim rng As Range, aStr As String, bStr As String
For Each rng In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
If rng.Value <> "" Then
aStr = rng.Value
bStr = rng.Offset(, 1).Value
rng.Value = bStr
rng.Offset(, 1).Value = aStr
bStr = ""
aStr = ""
End If
Next
End Sub
 
Upvote 0
Try this with a copy of your workbook.
Code:
Sub Swap()
  Dim a As Variant, tmp As Variant
  Dim i As Long
  
  With Range("A1", Range("A" & Rows.Count).End(xlUp)).Resize(, 2)
    a = .Value
    For i = 1 To UBound(a)
      If Len(a(i, 1)) > 0 Then
        tmp = a(i, 1)
        a(i, 1) = a(i, 2)
        a(i, 2) = tmp
      End If
    Next i
    .Value = a
  End With
End Sub
 
Upvote 0
can col B be empty and col A have a value? Can you try the below?

Code:
Sub swap()
Dim rng As Range, aStr As String, bStr As String
For Each rng In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
If rng.Value <> "" Then
aStr = rng.Value
bStr = rng.Offset(, 1).Value
rng.Value = bStr
rng.Offset(, 1).Value = aStr
bStr = ""
aStr = ""
End If
Next
End Sub

Thanks worked 100%
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
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