VBA to move the rightmost non-empty cell into column F

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
801
Office Version
  1. 365
Platform
  1. Windows
I have a table of data which contains address fields across columns C to F.

Column C will always be the first line of the address which is fine, however, the postcode, which needs to be in column F, can be in either D, E or F.

What I need is code that can start with a Do Until, end in a Loop, like this:

Do Until Cells(ActiveCell.Row, "A").Value = ""

<Code Needed Here>


Loop

So it will go down until it detects there is no value in A there it will stop. For every value in A, it will select the rightmost non-empty cell from D to F, cut it, then paste it in F.

Maybe with an Offset(0,1) function?


Here's what I have so far but it's not working...

Do Until Cells(ActiveCell.Row, "A").Value = ""
If Range("E") = "" Then
Range(Cells(ActiveCell.Row, "D")).Cut
Range(Cells(ActiveCell.Row, "F")).PasteSpecial xlValues
ActiveCell.Offset(0, 1).Activate


If Range("F") = "" Then
Range(Cells(ActiveCell.Row, "E")).Cut
Range(Cells(ActiveCell.Row, "F")).PasteSpecial xlValues


ActiveCell.Offset(1, 0).Activate
Loop
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Ah I got it, like this:

Code:
Range("A2").Activate
Do Until Cells(ActiveCell.Row, "A").Value = ""
    If Cells(ActiveCell.Row, "E").Value = "" Then
    Cells(ActiveCell.Row, "F").Value = Cells(ActiveCell.Row, "D").Value
    Cells(ActiveCell.Row, "D").ClearContents
    End If


    If Cells(ActiveCell.Row, "F").Value = "" Then
    Cells(ActiveCell.Row, "F").Value = Cells(ActiveCell.Row, "E").Value
    Cells(ActiveCell.Row, "E").ClearContents
    End If


    ActiveCell.Offset(1, 0).Activate
Loop
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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