View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.newusers
AltaEgo AltaEgo is offline
external usenet poster
 
Posts: 245
Default Need a macro to copy cells

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

--
Steve

"OssieMac" wrote in message
...
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?

--
Regards,

OssieMac


"OssieMac" wrote:

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?


--
Regards,

OssieMac


"pcor" wrote:

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