Automatically copy cell if blank with vba

  • Hello can someone help me with vba code, let's say I have a sheet with 2 columns. If 1st cell in column A is blank, i want to automatically copy 1st cell in column B and paste it in 1st cell in column A, etc, BUT if cells in column A is NOT blank, i want to keep column A with the current data. DO I even need vba code to do this? :D
    ANY and all help is GREATLY appreciated. tHANKYOU ^^

  • Here's a non-VBA way:


    1) Highlight Column A
    2) Select all blank cells (Ctrl-G should get you to the "Go To" box, then click "Special", click "Blanks", and "Okay")
    3) All blank cells should be highlighted
    4) Type "=" and then arrow to the right of the blank box (so if the first blank box was A1, you would now have "=B1") (don't click with the mouse anywhere otherwise all the blanks will be unselected)
    5) Press Ctrl + Enter
    6) If done correctly, it'll fill all blank cells with the contents of the adjacent cell in column B


    I'm sure someone has a better way, but I have found this useful many times. Hope this works for you.

  • Code
    Sub With_Loop()
    Dim c As Range, lr As Long
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    If WorksheetFunction.CountA(Range("A:A")) = lr Then MsgBox "No empty cells in column A!": Exit Sub
        For Each c In Range("A2:A" & lr).SpecialCells(4)
            c.Value = c.Offset(, 1).Value
        Next c
    End Sub


    Following leaves a formula in the previous empty cells.

    Code
    Sub With_Formula()
    Dim lr As Long
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    If WorksheetFunction.CountA(Range("A:A")) = lr Then MsgBox "No empty cells in column A!": Exit Sub
    With Range("A2:A" & lr).SpecialCells(4)
        .Formula = "=RC[1]"
    End With
    End Sub

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!