简体   繁体   中英

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!

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

   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM