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

Dan87

New Member
Joined
Jun 25, 2012
Messages
3
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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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?
 
Upvote 0
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:
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,219,162
Messages
6,146,661
Members
450,706
Latest member
LGVBPP

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.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

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

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

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
Back
Top