'In Excel, how to replace value from column 2 into column 1 if column 2 cell is not empty

I have 2 columns in an Excel table. Column 1 is fully populated. Column 2 is populated with updated values for column 1. Not all cells in column 1 will be replaced. If the corresponding cell in column 2 is empty, then no change is needed. There are 6000 rows to change so I am looking for formula to do the work. Here is an example of the table:

Column 1 Column 2
Data 1 New Data 1
Data 2
Data 3 New Data 3

Convert to this:

Column 1 Column 2
New Data 1 New Data 1
Data 2
New Data 3 New Data 3

Is there a way to do this with a formula or built in function?

Thanks!



Solution 1:[1]

Suppose "Column 1" is in column C and "Column 2" is in column D.

METHOD 1: Let's say you have "Data 1" in cell "D2" and "New Data 1" in cell "C2". Then you can use the funtion "IF" and the function "ISBLANK" to get a new column with the results (and then you scroll down the function):

=IF(ISBLANK(D2);C2;D2)

METHOD 2: If instead you want to replace the data on the same column (C), you have to run a loop in VBA like this one (please replace "SheetName" with your sheet name):

Sub Worksheet_SelectionChange()
    x = Application.WorksheetFunction.CountA(Worksheets("SheetName").Range("C1:C10000"))
    Worksheets("SheetName").Select
    For i = 1 To x:
        If Not IsEmpty(Cells(i, "D").Value) Then
            Cells(i, "C").Value = Cells(i, "D").Value
        End If
    Next i
End Sub

   

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1