Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default Autosum function

I have a list of numbers with all in same column. When clicking normal
autosum function, Excel determines the sum range for me, including subtotals
& grandtotals.
How can this happen with same effect in VBA language so that I need not
define the sum ranges (variable).

E.g.

1
2
3
click autosum : =sum(1:3)
4
5
click autosum : =sum(4:5)
click autosum : =sum(above subtotals)
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 216
Default Autosum function

Sub Test()
Dim iLastRow As Long
Dim i As Long
Dim tmpSub As Double, tmpTotal As Double

iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
tmpSub = Range("A1").Value
For i = 2 To iLastRow + 1
If Cells(i, "A") < "" Then
tmpSub = tmpSub + Cells(i, "A").Value
Else
Cells(i, "A").Value = tmpSub
tmpTotal = tmpTotal + tmpSub
tmpSub = 0
End If
Next i
Cells(i, "A").Value = tmpTotal

End Sub


--
HTH

Bob Phillips

(remove xxx from email address if mailing direct)

"Frankie" wrote in message
...
I have a list of numbers with all in same column. When clicking normal
autosum function, Excel determines the sum range for me, including

subtotals
& grandtotals.
How can this happen with same effect in VBA language so that I need not
define the sum ranges (variable).

E.g.

1
2
3
click autosum : =sum(1:3)
4
5
click autosum : =sum(4:5)
click autosum : =sum(above subtotals)



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 216
Default Autosum function

If you want formulae, try

Sub Test()
Dim iLastRow As Long
Dim i As Long
Dim iStart As Long
Dim tmp As String

iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
iStart = 1
For i = 2 To iLastRow + 1
If Cells(i, "A") = "" Then
Cells(i, "A").Formula = "=SUM(A" & iStart & ":A" & i - 1 & ")"
tmp = tmp & "A" & i & ","
iStart = i + 1
End If
Next i
Cells(i, "A").Formula = "=SUM(" & Left(tmp, Len(tmp) - 1) & ")"

End Sub

--
HTH

Bob Phillips

(remove xxx from email address if mailing direct)

"Bob Phillips" wrote in message
...
Sub Test()
Dim iLastRow As Long
Dim i As Long
Dim tmpSub As Double, tmpTotal As Double

iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
tmpSub = Range("A1").Value
For i = 2 To iLastRow + 1
If Cells(i, "A") < "" Then
tmpSub = tmpSub + Cells(i, "A").Value
Else
Cells(i, "A").Value = tmpSub
tmpTotal = tmpTotal + tmpSub
tmpSub = 0
End If
Next i
Cells(i, "A").Value = tmpTotal

End Sub


--
HTH

Bob Phillips

(remove xxx from email address if mailing direct)

"Frankie" wrote in message
...
I have a list of numbers with all in same column. When clicking normal
autosum function, Excel determines the sum range for me, including

subtotals
& grandtotals.
How can this happen with same effect in VBA language so that I need not
define the sum ranges (variable).

E.g.

1
2
3
click autosum : =sum(1:3)
4
5
click autosum : =sum(4:5)
click autosum : =sum(above subtotals)





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default Autosum function

Hi Bob,

It solves part of my problem as it only gives one subtotal and one grandtotal.
In fact, the spreadsheet has 2 columns, column "A" contains wordings "total
/ subtotal" and column "B" list of values. My job is to find where the
total/subtotal located in column "A" and create an autosum formula in
corresponding cell in column "B". Since the sum ranges vary, I can only
locate the total/subtotal manually in column "A" & click autosum in column
"B" where the sum ranges appear automatically. However, I don't know how to
automate this process in VBA. Eager to hear your advice on it.

Rgds,

"Bob Phillips" wrote:

If you want formulae, try

Sub Test()
Dim iLastRow As Long
Dim i As Long
Dim iStart As Long
Dim tmp As String

iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
iStart = 1
For i = 2 To iLastRow + 1
If Cells(i, "A") = "" Then
Cells(i, "A").Formula = "=SUM(A" & iStart & ":A" & i - 1 & ")"
tmp = tmp & "A" & i & ","
iStart = i + 1
End If
Next i
Cells(i, "A").Formula = "=SUM(" & Left(tmp, Len(tmp) - 1) & ")"

End Sub

--
HTH

Bob Phillips

(remove xxx from email address if mailing direct)

"Bob Phillips" wrote in message
...
Sub Test()
Dim iLastRow As Long
Dim i As Long
Dim tmpSub As Double, tmpTotal As Double

iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
tmpSub = Range("A1").Value
For i = 2 To iLastRow + 1
If Cells(i, "A") < "" Then
tmpSub = tmpSub + Cells(i, "A").Value
Else
Cells(i, "A").Value = tmpSub
tmpTotal = tmpTotal + tmpSub
tmpSub = 0
End If
Next i
Cells(i, "A").Value = tmpTotal

End Sub


--
HTH

Bob Phillips

(remove xxx from email address if mailing direct)

"Frankie" wrote in message
...
I have a list of numbers with all in same column. When clicking normal
autosum function, Excel determines the sum range for me, including

subtotals
& grandtotals.
How can this happen with same effect in VBA language so that I need not
define the sum ranges (variable).

E.g.

1
2
3
click autosum : =sum(1:3)
4
5
click autosum : =sum(4:5)
click autosum : =sum(above subtotals)






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How 2 sort an autosum total list after adding items 2 autosum item akm Excel Discussion (Misc queries) 0 May 30th 10 11:44 PM
The copy down function for autosum gives wrong answers franky Excel Discussion (Misc queries) 4 January 6th 09 12:34 AM
Autosum function not working Jerry[_5_] Excel Discussion (Misc queries) 1 March 12th 08 12:14 PM
Where is the result after I do an autosum function? PatS Excel Discussion (Misc queries) 14 January 20th 08 05:22 PM
Autosum function disappeared/knocked out Simon Ryan Excel Discussion (Misc queries) 2 December 16th 05 03:48 PM


All times are GMT +1. The time now is 03:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"