borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Justify)Next Topic (Word 2000 - UserForm behavior) New Topic New Poll Post Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : How to sum by macro
Poster Message
jonybd
Level: Master

Registered: 18-01-2005
Posts: 115

icon How to sum by macro

'I click the caculate button and how to sum E4 till End of records.

Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)

End Sub


____________________________
Telecommunication programmer.
Sip, Isdn - technology.

____________________________
Attached:
excel.jpg (34 KB)

19-06-2005 at 06:19 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
jonybd
Level: Master

Registered: 18-01-2005
Posts: 115
icon Re: How to sum by macro

this is something i want to do (manualy ) but where E5 can not be fixed (for automatic) .
For Each cl In Range("E4", "E5").Cells
and
Sheets("Sheet1").Range("E6").Value = TempSum
it has to be not E6 it has to be E(end of original records counted).

Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)

    Dim cl As Range, TempSum As Double
        ' Application.Volatile ' this is optional
        TempSum = 0
        
        On Error Resume Next ' ignore cells without values
        
        For Each cl In Range("E4", "E5").Cells
                TempSum = TempSum + cl.Value
        Next cl
        
        Set cl = Nothing
        Sheets("Sheet1").Range("E6").Value = TempSum
        
End Sub


[Edited by jonybd on 19-06-2005 at 07:01 AM GMT]

____________________________
Telecommunication programmer.
Sip, Isdn - technology.

19-06-2005 at 06:58 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
neutrall
Level: Master


Registered: 28-03-2004
Posts: 122
icon Re: How to sum by macro

Here a quick function I did for you.  

Function FloatSum(XRow As Integer, YClm As Integer)
    Dim CurrRow As Integer
    Dim TmpHolder As Integer
    
    CurrRow = XRow
    TmpHolder = 0
    Do While ((IsNumeric(Sheet1.Cells(CurrRow, YClm).Value)) And (Trim(Sheet1.Cells(CurrRow, YClm).Value) <> ""))
        TmpHolder = TmpHolder + CInt(Sheet1.Cells(CurrRow, YClm).Value)
        CurrRow = CurrRow + 1
    Loop
    
    FloatSum = TmpHolder
    
End Function


I think that this is what you wanted. To use the function, just give it the stating cell coordinated.

EX : D5 = (5,4)

The function will return the sum of all the number following the cell until it meet with a non numeric value or a empty cell.

Daniel B.

____________________________
A Stick give a wise man something to think about... and a fool, something to put in is mouth.

23-06-2005 at 12:58 AM
View Profile Send Email to User Show All Posts Visit Homepage ICQ | Quote Reply
~Bean~
Level: VB Guru


Registered: 07-04-2003
Posts: 488
icon Re: How to sum by macro

You could also use UsedRange which will keep going regardless of any empty cells...


For cntRow = 4 To ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
myTotal = myTotal + ActiveSheet.Cells(cntRow,5).Value
Next cntRow


____________________________
Eggheads unite! You have nothing to lose but your yolks.

30-07-2005 at 01:31 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
googoojee
Level: Trainee

Registered: 04-10-2006
Posts: 2
icon Re: How to sum by macro

how can you apply the function into VB , i have the same problem but when i copy and paste your solution in VB it doesnot work.

Function FloatSum(XRow As Integer, YClm As Integer)
    Dim CurrRow As Integer
    Dim TmpHolder As Integer
    
    CurrRow = XRow
    TmpHolder = 0
    Do While ((IsNumeric(Sheet1.Cells(CurrRow, YClm).Value)) And (Trim(Sheet1.Cells(CurrRow, YClm).Value) <> ""))
        TmpHolder = TmpHolder + CInt(Sheet1.Cells(CurrRow, YClm).Value)
        CurrRow = CurrRow + 1
    Loop
    
    FloatSum = TmpHolder
    
End Function

** with the example at the top, how can i substitue the rows and columns in your VB program?

help from a VB beginner

04-10-2006 at 11:44 PM
View Profile Send Email to User Show All Posts | Quote Reply
Sahbun
Level: Trainee

Registered: 16-10-2006
Posts: 1
icon Re: How to sum by macro

Hi, try this......................

Sub sumrow()

Dim c As Long
Dim sm As Double
sm = 0
For c = 1 To Cells.count
    If Sheets("Test").Cells(c, 1).Value = "" Then
        Sheets("Test").Cells(c, 1).Value = sm
        Exit For
    Else
       sm = sm + Sheets("Test").Cells(c, 1).Value
    End If

Next c

End Sub

16-10-2006 at 03:51 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VBA (Access, Excel, Word, ...) : How to sum by macro
Previous Topic (Justify)Next Topic (Word 2000 - UserForm behavior) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder