Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 19th, 2010, 01:29 AM
Registered User
 
Join Date: May 2010
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Worksheet function Sum in Excel VBA

Hi,

I have query using worksheet function in excel vba, I have written the code, but Its not getting the results, I am looking some particular columns to do sum and output should be on desired column, since I will be updating the data in same columns in dayin & dayout, below is the example and code which I prepared.

Example:-

A1,B1,C1,D1,E1,F1 - ------ J1(sum of A1,c1,d1,f1)
.
.
.
.
.
A100,b100,c100,d100,e100,f100-------J100(sum of a100,c100,d100,f100)

Code:

PHP Code:
Sub Sum_Click()
Dim lastrow As Long
lastrow 
Cells(Rows.Count"A").End(xlUp).Row
Range
("J1:J" lastrow).Formula Application.WorksheetFunction.Sum(Range("a1:a" lastrow), Range("c1:c" lastrow), Range("d1:d" lastrow), Range("f1:f" lastrow))
End Sub 

I would request any one of you to help me correcting this code.

Thanks in advance.

Regards
GV Reddy
 
Old August 19th, 2010, 01:48 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

the worksheet function will return a 'value'. Since you want a forumla in each cell you should use something like :

Range("J1:J" & lastrow).Formula = "=Sum(a1:a & cstr(lastrow),)

Cheers
Shasur


__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old August 19th, 2010, 02:01 AM
Registered User
 
Join Date: May 2010
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

In the above mentioned code formula copies in cell,Since i have huge data, size of the excel will increase drastically if i follw this code.

Regards
GV Reddy
 
Old August 19th, 2010, 02:44 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Check the following:

Code:
Sub Sum_1()
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
For iRow = 1 To lastrow
    Range("J" & iRow).Value = Application.WorksheetFunction.Sum(Range("a" & iRow), Range("c" & iRow), Range("d" & iRow), Range("f" & iRow))
Next iRow
End Sub
Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Pop-ups and arguments descriptions in VBA Function (Excel 2010) sektor Excel VBA 2 May 28th, 2010 04:52 AM
Translating WorkSheet Formula in VBA AndyL Beginning VB 6 2 June 11th, 2008 04:13 AM
Calling MS Access vba function via Excel Kourosha Excel VBA 0 December 31st, 2007 08:33 AM
TRIM, ISTEXT function won't work in Excel VBA code munzer1 Excel VBA 1 May 29th, 2007 09:35 AM
Excel 2003 VBA Function RollingWoodFarm Excel VBA 15 August 2nd, 2006 04:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.