简体   繁体   中英

Excel VBA to copy cell value if > 0 to cell to the left

In Column W I have BALANCE2 values & In Column X I have BALANCE3 values

I need to move the values from BALANCE3 (which are greater than zero) to overwrite the corresponding cell in BALANCE2 so that I have one total list of payments.

Can anyone help to work with this one? I don't know how to do it.

Do not use a macro for this

Just use excel's built in IF , for example, assuming BALANCE2 is column W and BALANCE3 is column X , write in cell Z1

=IF(X1>0,X1,W1)

And then drag it down to the entire column, cut and paste column Z into column W , and you got your "overwrite" without writing macro code

Try this one:

Public Sub overwriteValues()

    Dim row As Integer

    'Set start row
    row = 1

    With Sheets("sheetname")

        'Loop until column X cell is blank
        'You can modify column if you want other
        Do While .Range("X" & row) <> ""

            If .Range("X" & row) > 0 Then

                .Range("W" & row) = .Range("X" & row)

            End If

            'Increase row
            row = row + 1

        Loop

    End With

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