Google Answers Logo
View Question
 
Q: Simple Excel 2000 VBA ( Answered,   0 Comments )
Question  
Subject: Simple Excel 2000 VBA
Category: Computers > Programming
Asked by: eller-ga
List Price: $5.00
Posted: 22 Nov 2002 09:31 PST
Expires: 22 Dec 2002 09:31 PST
Question ID: 112601
I have two 3x3 (for simplicity's sake) ranges of cells; all cells in
each range contain plain ol' integers. I want to add the values of the
cells in Range A to the values of the cells in Range B and have the
sums become the new values of the cells in Range A.

I am learning VBA 'by ear' so I'm not very good, but long ago I
figured I could perform such an operation with SINGLE cells simply by
something like:

Range("A1").Value = Range("A1").Value + Range("A2").Value

However, when I made Range reference multiple cells...

Range("A1:C3").Value = Range("A1:C3").Value + Range("A4:C6").Value

...I got a type mismatch.

I then figured out that I could use Copy and PasteSpecial(xlValues) to
attain the desired effect, but in practice the ranges are large and
the calculations are repeated hundreds of times. This abuse of the
clipboard really wreaks havoc on system performance and I'm sure
there's a better way to do it.

I'd like specific examples of VBA code that would help me accomplish
the seemingly simple task of adding two ranges together. I imagine
there's more than one solution, and I'd like to see all possibilities,
but obviously the most streamlined approaches are preferred.

By the way, another thing that's been bugging me is the redundancy in
my examples. If you can show me a way to add two values without
mentioning the 'destination' range twice, I'd gladly toss in an extra
few bucks.

Thanks.

Clarification of Question by eller-ga on 22 Nov 2002 10:00 PST
Immediately after posting I stumbled onto the Consolidate method and
thought I had figured it out. Unfortunately, Consolidate apparently
won't work if the source and destination overlap. Still, it's very
close to what I need, so I suppose an alternate question would be if
there are any convenient ways around this limitation so that
Consolidate can be used.
Answer  
Subject: Re: Simple Excel 2000 VBA
Answered By: hammer-ga on 22 Nov 2002 10:23 PST
 
Here are a couple of different ways to do this. The second way is much
more flexible. I recommend you use it for anything at all complicated.
It uses Excel's ability to refer to cells by row and column index. See
Excel Help for more information on the Cells collection.

Public Sub SumMe()
Dim xx As Integer

    For xx = 1 To 3
	' The following statement is all one line. 
        Range("A" & CStr(xx)).Value = Range("A" & CStr(xx)).Value +
Range("B" & CStr(xx)).Value + Range("C" & CStr(xx)).Value
    Next xx

End Sub

*****************************************************************

Public Sub SumMeAgain()
Dim intRow As Integer
Dim intCol As Integer
Dim intSum As Integer

    For intRow = 1 To 3
        intSum = 0
        For intCol = 1 To 3
            intSum = intSum + Cells(intRow, intCol)
        Next intCol
        Cells(intRow, 1).Value = intSum
    Next intRow

End Sub
****************************************************

Good luck with your project!

- Hammer
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy