![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Board Regular
Join Date: Mar 2002
Location: Boston, MA
Posts: 105
|
I have a macro in which I need to sum up several rows individually. My problem is that I do not know how many columns will be there ahead of time. Is there a way to use VBA to tell Excel just to click the Sum button in the toolbar?
As a second question on the same topic, is there a way to pass a variable into a SUM formula as in this example: ActiveCell.FormulaR1C1 = "=SUM(RC[-LastColumn],RC[-1])" where LastColumn is a variable defined earlier |
|
|
|
|
|
#2 |
|
Banned
Join Date: Feb 2002
Posts: 1,582
|
Hi jrnyman
You can click the Sum button via VBA using the Excecute Property, but you may find this way a bit better. It will place a Sum at the end a list in Column A : E Sub SumCols() Dim i As Integer For i = 1 To 5 Columns(i).Range("A65536").End(xlUp).Offset(1, 0).Formula _ = "=Sum($A$1:" & Columns(i).Range("A65536").End(xlUp).Address & ")" Next i End Sub |
|
|
|
|
|
#3 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Bogota, Colombia
Posts: 11,927
|
If you have selected the cell where you want the sum to appear, you can use this macro to "simulate" the clicking on the AutoSum icon. Worked on 2000.
Sub AutoSumClick() Application.CommandBars.FindControl(ID:=226).Execute Application.CommandBars.FindControl(ID:=226).Execute End Sub That way you don't need to know what row are you in, just select it. |
|
|
|
|
|
#4 |
|
Board Regular
Join Date: Mar 2002
Location: North Wales Coast, UK
Posts: 95
|
Juan,
Thank you for posting that code .. most excellent I have been trying to find out where you got the Button ID number from, without success! Do you happen to know if there is a 'problem' running your posted code in Excel 2002? I was somewhat surprised, I thought it would have been 'upwardly' compatible. Regards S |
|
|
|
|
|
#5 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
|
How about:
|
|
|
|
|
|
#6 |
|
Board Regular
Join Date: Mar 2002
Location: North Wales Coast, UK
Posts: 95
|
Thank you Mark,
I may even rush into work tomorrow to try that code! There again .. Lol Cheers S |
|
|
|
|
|
#7 |
|
Board Regular
Join Date: Mar 2002
Location: North Wales Coast, UK
Posts: 95
|
After rushing into work this morning .. alas the revised suggestion did not work in Excel 2002
Anybody any other ideas? S |
|
|
|
|
|
#8 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
|
How about trying Daves OR
Sub AutoSum() CommandBars.FindControl(ID:=226).Execute Application.SendKeys "~" End Sub |
|
|
|
|
|
#9 |
|
Board Regular
Join Date: Feb 2002
Posts: 3,065
|
the code of Ivans works well this i use..
__________________
Free Excel based Web Toolbar available here. Jack in the UK J & R Excel Solutions "making Excel work for you" |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|