Swap Values for Entire column

Devbox777

New Member
Joined
Aug 18, 2015
Messages
13
Hi guys,

I've been working on a small vba macro that will swap the values in column a and b if column a is greater.

I already came up with the code to swap values between two cells...but I need this code to work for the whole column of a and b. I suppose I need a loop...can someone help me with this? I appreciate it.

here is my existing code.

Code:
Sub Transpose()
Dim column1 As Integer
Dim column2 As Integer

column1 = Range("A1").Value
column2 = Range("B1").Value


If column1 > column2 Then

Range("A1").Value = Range("B1").Value
Range("B1").Value = column1

Else: MsgBox "Completed"

End If


End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Consider:

Code:
Sub Transpose()
   Dim column1 As Integer
   Dim column2 As Integer
   Dim N As Long, i As Long
   N = Cells(Rows.Count, "A").End(xlUp).Row
   
   For i = 1 To N
      column1 = Range("A" & i).Value
      column2 = Range("B" & i).Value
      If column1 > column2 Then
         Range("A" & i).Value = column2
         Range("B" & i).Value = column1
      End If
   Next i
End Sub
 
Upvote 0
Hi,

This code works!! But I do have a couple more conditions I was trying to use in this code as well...in column B..some cells have the word "NULL" is there a way to add an ElseIf statement to copy the value in Column A to B if the value is NULL? I tried to implement it myself and I'm getting errors:

Code:
Sub Transpose()

   Dim column1 As Integer
   Dim column2 As Integer
   Dim N As Long, i As Long
   N = Cells(Rows.Count, "A").End(xlUp).Row
   
   For i = 1 To N
      column1 = Range("A" & i).Value
      column2 = Range("B" & i).Value
      
      If column1 > column2 Then
         Range("A" & i).Value = column2
         Range("B" & i).Value = column1
      
      'ElseIf column2 = "NULL" Then
        
        'Range("A" & i).Value = column2
      
      End If
   Next i

2. Also, some of the cells in column B are blank...I want to copy the values of column A to B if this happens as well...

I appreciate the help!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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