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
FPE100
<strike></strike>
<strike></strike>
FPE101
<strike></strike>
FPA001
<strike></strike>
FPO00111
FPE102
<strike></strike>
<strike></strike>
FPE103

<tbody>
</tbody>
<strike></strike>
So the response should be:
FPE100
<strike></strike><strike></strike>
FPE101
FPO00111
<strike></strike>
FPA001
<strike></strike><strike></strike>
FPE102
<strike></strike><strike></strike>
FPE103
<strike></strike>

<tbody>
</tbody>
<strike></strike><strike></strike>
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
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,214,642
Messages
6,120,700
Members
448,979
Latest member
DET4492

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