PDA

View Full Version : [SOLVED] Adding two cells together



Dio
10-11-2008, 08:14 PM
Hi,

I am supposed to write a macro for work and until I get more familiar with VBA I decided to just write one that did something simple.

By using the Record Macro and some example scripts I got some of it down. But for some reason my code doesn't work. Here's what I have:



Option Explicit

Sub Addition()
Dim Add As Integer
Dim var1 As Integer
Dim var2 As Integer
var1 = Range("A2").Select
var2 = Range("B2").Select
Add = var1 + var2
Range("C2").Select
ActiveCell.FormulaR1C1 = Add
End Sub


It displays -2 in cell 'C2' instead of the correct answer which would be 30
(A2 = 20, B2 = 10)

jolivanes
10-11-2008, 08:36 PM
This will give you 30 as an answer.



Sub Addition()

Dim Add As Integer
Dim var1 As Integer
Dim var2 As Integer
var1 = Range("A2").Value
var2 = Range("B2").Value
Add = var1 + var2
Range("C2").Select
ActiveCell.FormulaR1C1 = Add

End Sub

Dio
10-11-2008, 08:39 PM
Thanks!

lucas
10-12-2008, 05:38 AM
Alternative:


Sub add()
Range("C2").Value = Range("A2").Value + Range("B2").Value
End Sub


Be sure to mark your thread solved using the thread tools at the top of the page when you are sure you have an answer.

Bob Phillips
10-12-2008, 06:28 AM
Use Excel


Sub add()
Range("C2").FormulaR1C1 = "=SUM(RC[-2]:RC[-1])"
End Sub