Need a macro to copy cells

P

pcor

I have numbers spread thruout col A, with many blank cells in between the data.
As time goes on I will add numbers in col B. In most cases there could be a
number in col B where there is none in col A(adjacent cell)
I would like to have a macro copy the numbers from col B to col A. If there
already is a number in col A I would like the number in col a to be added to
the number in col B
Thanks
 
O

OssieMac

Need to confirm a couple of thing.

"If there already is a number in col A I would like the number in col a to
be added to the number in col B". Do you mean if 5 exists in col A and you
insert 10 in col B then col B becomes 15?

What is to occur if there is an existing number in col B and you edit it?
 
P

pcor

Once the daily input in Col B was made and the data moved to Col A, the data
in col B would be erased- no editing would take place
 
O

OssieMac

You didn't answer my question on the following.

"If there already is a number in col A I would like the number in col A to
be added to the number in col B". Do you mean if 5 exists in col A and you
insert 10 in col B then col B becomes 15? Or do you mean that col A becomes
15?
 
A

AltaEgo

Perhaps the OP solved the problem? Irrespective, I am also having problems
understand the logic of the request.

My interpretation of the question
1) If A is blank add B to A and store in A. {my comment - delete Column B
value?}

2) If there is a number in A, add it to column B. {my comment - this seems
reverse logic of the above BUT, the OP may have a good reason for doing it
this way and clearly seems to indicate copy rather than add}

Code to do this:

Sub AddBtoAorAtoB()
Dim rws As Long

rws = ActiveSheet.UsedRange.Rows.Count


For i = 1 To rws
If ActiveSheet.Range("A" & i) > 0 Then
If ActiveSheet.Range("B" & i) & "" = "" Then
ActiveSheet.Range("B" & i) = ActiveSheet.Range("A" & i)
Else
ActiveSheet.Range("A" & i) = ActiveSheet.Range("B" &
i).Value + ActiveSheet.Range("A" & i)
End If
Else
ActiveSheet.Range("A" & i) = ActiveSheet.Range("B" & i).Value
End If
Next i

End Sub



If the OP just wants to add the value in column B to the value in column A,
whether either of the values is blank is irrelevant:

Sub SimplyAddBtoA()
Dim rws As Long

rws = ActiveSheet.UsedRange.Rows.Count


For i = 1 To rws

ActiveSheet.Range("A" & i) = ActiveSheet.Range("B" & i).Value +
ActiveSheet.Range("A" & i)
'uncomment below to prevent 0 values
'If ActiveSheet.Range("A" & i) = 0 Then ActiveSheet.Range("A" & i) =
""
'to prevent repeated adding of column B values
' clean up as you go
ActiveSheet.Range("B" & i).Value = ""


Next i

End Sub


The ball is in pcor's court. If both the above missed the specification,
pcor may help by supplying sample data (this is how it looks like now, this
is how it should look after the code runs) .
 
P

pcor

If 5 exists in col A and 10 is inserted in col B, I would like the macro to
make col A =15
Sorry for the confusion. and Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top