# If there is a value in a certain column range, copying it to same row in another column

#### Dan87

##### New Member
Hi All,

Before I state my problem I just want to say that I have learned alot from this forum and thanks in advance to whoever answers!

So I've looked around the forums and I've not really found a solution which works exactly.

Say my sheet is set up as below
A B C
1 AAA 12 0
2 BBB 14 0
3 CCC 0 5
4 DDD 2 4

Column C has formulas the whole way down that either returns zero or a value. In the instances where there are values, I want the code to copy the value from column C and paste it into the respective row in column B. If the Value in column C is 0, then I want the value is column B to remain as is.

I'm not sure how to approach this problem, some sort of loops which goes through and checks would probably be the best way but I'm not sure how to proceed. Any input would be much appreciated!

### Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Solving this with a formula the way you suggest may cause circular references. Instead, why not add column D:
=if(c1>0,c1,b1)
Then hide column B?

Hi All,

Before I state my problem I just want to say that I have learned alot from this forum and thanks in advance to whoever answers!

So I've looked around the forums and I've not really found a solution which works exactly.

Say my sheet is set up as below
A B C
1 AAA 12 0
2 BBB 14 0
3 CCC 0 5
4 DDD 2 4

Column C has formulas the whole way down that either returns zero or a value. In the instances where there are values, I want the code to copy the value from column C and paste it into the respective row in column B. If the Value in column C is 0, then I want the value is column B to remain as is.

I'm not sure how to approach this problem, some sort of loops which goes through and checks would probably be the best way but I'm not sure how to proceed. Any input would be much appreciated!

This per the OP:

Code:
Sub moveValue()
Dim sh As Worksheet, lr As Long, rng As Range
Set sh = ActiveSheet
lr = sh.Cells(Rows.Count,3).End(xlUp).Row
Set rng = sh.Range("C2:C" & lr)
For Each c In rng
If c > 0 Then
c.Ofset(0, -1) = c.Value
End If
Next
End Sub
Code:

This might be what was meant:

Code:
Sub moveValue()
Dim sh As Worksheet, lr As Long, rng As Range
Set sh = ActiveSheet
lr = sh.Cells(Rows.Count,3).End(xlUp).Row
Set rng = sh.Range("C2:C" & lr)
For Each c In rng
If c > 0 Then
c.Ofset(0, -1) = c.Value
c = 0
End If
Next
End Sub
Code:

Here is a non-looping macro for you to consider...

Code:
``````Sub ReplaceColBwithNonZeroColC()
Dim UnusedColumn As Long, LastRow As Long
Const StartRow As Long = 1
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
UnusedColumn = Cells.Find(What:="*", SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, LookIn:=xlFormulas).Column + 1
With Cells(StartRow, UnusedColumn).Resize(LastRow - StartRow + 1)
.FormulaR1C1 = "=IF(RC3=0,RC2,RC3)"
.Offset(, 2 - UnusedColumn).Value = .Value
.Clear
End With
End Sub``````

This per the OP:

Code:
Sub moveValue()
Dim sh As Worksheet, lr As Long, rng As Range
Set sh = ActiveSheet
lr = sh.Cells(Rows.Count,3).End(xlUp).Row
Set rng = sh.Range("C2:C" & lr)
For Each c In rng
If c > 0 Then
c.Ofset(0, -1) = c.Value
End If
Next
End Sub
Code:

Code:

Thanks for the code JLGwhiz, this works to transfer the values to the right column, however it replaces the values in B with 0 if the value in C is zero, whereas if C is 0, I want it to leave the original value in B. Would you have a workaround for that? I'm guessing some kind of IF statement but I don't seem to be getting it

Ignore! Works perfectly thank you everyone for your input!

Replies
8
Views
771
Replies
16
Views
647
Legacy 143009
L
Replies
2
Views
191
Replies
2
Views
421
Replies
7
Views
336

1,212,150
Messages
6,106,257
Members
448,009
Latest member
wbarkwell

### 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.

### Which adblocker are you using?

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

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