Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple vb code for adding columns together

Status
Not open for further replies.

wafs

Technical User
Jan 17, 2006
112
US
I need to add a few columns together and it is not working.

Total=Sum("R2:AB2")

What am I doing wrong? I did a recorder to get this initial forumla, but it is not adding the columns together.
 
When you recorded it, that was a function on the worksheet. Typing [COLOR=blue white]=Sum("R2:AB2")[/color] in a cell will work fine. But SUM isn't a recognized function in VBA.

To get around this, you can use:
Code:
Total = WorksheetFunction.Sum(Range("R2:AB2"))

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ181-2886 before posting.
 
Thanks that works, but now i get a new error.


application-defined or object-defined error. Here is part of the code.

Dim total As String
Dim m As String
Dim n As String
Dim v As String
Dim y As Integer

'Build Daily Sheet
total = WorksheetFunction.Sum(Range("R2:AB2"))
'Get data From Rows
i = cellstart

Testrange2 = "A" + i 'parts column

For x = 1 To 4000
Error here ---> If UCase(Trim(sheets(sh).Range(total) > 0)) Then
 
What are you trying to do? I'm assuming that total is a number since it is the sum of a range.

What do you expect range(total) (or whatever number you come up with) to give you? Whatever you are expecting, this syntax won't work. It is equivalent to saying range(3). That just doesn't mean anything.

sheets(sh).Range(total) > 0 - Even if total was a proper range, you aren't doing anything in the range to compare to zero. Do you want to sum a range?

Even if you get figured out, sheets(sh).Range(total) > 0 is going to return TRUE or FALSE.

Why would you trim TRUE or FALSE?

Why would you be taking the upper case of TRUE or FALSE?

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ181-2886 before posting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top