PC Review


Reply
Thread Tools Rate Thread

Concatenating Formulas

 
 
Bob Zimski
Guest
Posts: n/a
 
      16th Jul 2009
My code below does exactly what I want for the first iteration, but the
second iteration doesn't yield what I want. Below shows what I would like the
result to be. The loop goes through the base column and adds pairs together.
However, I don't just want to add the pairs of numbers together, I want to
build the forumla up as what I show in the Formula Bar section. I am guessing
this has a lot more code behind it than what I have. I suspect that I have to
check if any or both cells have numbers only first and then create a formula
as I have done below. If there is a number and a formula then I would have to
build a different string, and finally if they are both formula's I would have
to replace the = with a + between the pairs.

Am I over complicating this?
The next question is, how do I pickup the formula, add to it with changes
and plunk it back in VB code.

Thanks

Bob


Base Result Formula Bar
2 9 =2+3+4
3 7 =3+4
4


Dim r As Integer
For r = 4 To 3 Step -1
Range("B" & r - 1).Formula = "=" & Range("A" & r - 1) & "+" & Range("A" & r)
Next r
 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      17th Jul 2009
I would do the code below. Using SUM and placing the dollar sign gets the
same reults as you method

LastRow = 20
Range("B2").formula = "=SUM(A2:A$" & LastRow & ")"
Range("B2").copy _
Destination:=Range("B2:B" & LastRow)


"Bob Zimski" wrote:

> My code below does exactly what I want for the first iteration, but the
> second iteration doesn't yield what I want. Below shows what I would like the
> result to be. The loop goes through the base column and adds pairs together.
> However, I don't just want to add the pairs of numbers together, I want to
> build the forumla up as what I show in the Formula Bar section. I am guessing
> this has a lot more code behind it than what I have. I suspect that I have to
> check if any or both cells have numbers only first and then create a formula
> as I have done below. If there is a number and a formula then I would have to
> build a different string, and finally if they are both formula's I would have
> to replace the = with a + between the pairs.
>
> Am I over complicating this?
> The next question is, how do I pickup the formula, add to it with changes
> and plunk it back in VB code.
>
> Thanks
>
> Bob
>
>
> Base Result Formula Bar
> 2 9 =2+3+4
> 3 7 =3+4
> 4
>
>
> Dim r As Integer
> For r = 4 To 3 Step -1
> Range("B" & r - 1).Formula = "=" & Range("A" & r - 1) & "+" & Range("A" & r)
> Next r

 
Reply With Quote
 
Bob Zimski
Guest
Posts: n/a
 
      17th Jul 2009
Thanks a nifty trick and someday I'm sure to use it. In this case I need the
results to be exactly as I portrayed as I need to retain =2+3+4 because I am
consolidating lines and I need to see the makeup of the components once I
consolidate the lines.

Bob

"Joel" wrote:

> I would do the code below. Using SUM and placing the dollar sign gets the
> same reults as you method
>
> LastRow = 20
> Range("B2").formula = "=SUM(A2:A$" & LastRow & ")"
> Range("B2").copy _
> Destination:=Range("B2:B" & LastRow)
>
>
> "Bob Zimski" wrote:
>
> > My code below does exactly what I want for the first iteration, but the
> > second iteration doesn't yield what I want. Below shows what I would like the
> > result to be. The loop goes through the base column and adds pairs together.
> > However, I don't just want to add the pairs of numbers together, I want to
> > build the forumla up as what I show in the Formula Bar section. I am guessing
> > this has a lot more code behind it than what I have. I suspect that I have to
> > check if any or both cells have numbers only first and then create a formula
> > as I have done below. If there is a number and a formula then I would have to
> > build a different string, and finally if they are both formula's I would have
> > to replace the = with a + between the pairs.
> >
> > Am I over complicating this?
> > The next question is, how do I pickup the formula, add to it with changes
> > and plunk it back in VB code.
> >
> > Thanks
> >
> > Bob
> >
> >
> > Base Result Formula Bar
> > 2 9 =2+3+4
> > 3 7 =3+4
> > 4
> >
> >
> > Dim r As Integer
> > For r = 4 To 3 Step -1
> > Range("B" & r - 1).Formula = "=" & Range("A" & r - 1) & "+" & Range("A" & r)
> > Next r

 
Reply With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      17th Jul 2009
Bob, what you are doing when you try to run the macro a second time is
simply putting the same formula in the same two cells by overwriting the
original.

You might want to try doing the math in code and then adding it to the cell
value by concatenation.

For R = 4 To 3 Step -1
Range("B" & r-1) = Range("B" & r-1).Value & "+" & _
Range("A" & r - 1) & "+" & Range("A" & r).Value
Next

This puts the numbers in as text and not a formula.

"Bob Zimski" <(E-Mail Removed)> wrote in message
news:30EA9C7F-38C5-429E-A36C-(E-Mail Removed)...
> My code below does exactly what I want for the first iteration, but the
> second iteration doesn't yield what I want. Below shows what I would like
> the
> result to be. The loop goes through the base column and adds pairs
> together.
> However, I don't just want to add the pairs of numbers together, I want to
> build the forumla up as what I show in the Formula Bar section. I am
> guessing
> this has a lot more code behind it than what I have. I suspect that I have
> to
> check if any or both cells have numbers only first and then create a
> formula
> as I have done below. If there is a number and a formula then I would have
> to
> build a different string, and finally if they are both formula's I would
> have
> to replace the = with a + between the pairs.
>
> Am I over complicating this?
> The next question is, how do I pickup the formula, add to it with changes
> and plunk it back in VB code.
>
> Thanks
>
> Bob
>
>
> Base Result Formula Bar
> 2 9 =2+3+4
> 3 7 =3+4
> 4
>
>
> Dim r As Integer
> For r = 4 To 3 Step -1
> Range("B" & r - 1).Formula = "=" & Range("A" & r - 1) & "+" & Range("A" &
> r)
> Next r



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
concatenating TED Microsoft Excel Worksheet Functions 3 13th Feb 2009 01:03 AM
CONCATENATING Welthey Microsoft Access VBA Modules 5 23rd Jun 2008 01:55 PM
Re: Need help concatenating and formulas Nick Hodge Microsoft Excel Misc 0 18th Dec 2006 10:12 PM
Concatenating two formulas =?Utf-8?B?TXVrZXNoIEdhcmc=?= Microsoft Excel Misc 4 5th Oct 2005 12:27 PM
Concatenating IF(AND formulas wellfm Microsoft Excel Misc 3 4th Oct 2005 07:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:21 AM.