Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi All
When a workbook opens, I am trying to loop through all 26 worksheets, adding up the numbers in range A1 of each sheet and displaying the result in a message box. I am failing miserably! One attempt shown below. Please let me know where I'm going wrong! Private Sub Workbook_Open() Dim WS_Count As Integer Dim I As Integer WS_Count = ActiveWorkbook.Worksheets.Count total = 0 For I = 1 To WS_Count ActiveWorkbook.Worksheets(I).Range("A1").Select nexttotal = Worksheet(I).Range("A1").Value total = total + nexttotal 'MsgBox ActiveWorkbook.Worksheets(I).Name Next I MsgBox total End Sub Many thanks in advance! Andy |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
This will do it for you. Nice and simple.
Sub AddRanges() Dim sh As Worksheet Dim i As Double For Each sh In Worksheets i = i + sh.Range("A1").Value Next sh MsgBox "Total = " & i End Sub Hope it helps! -- Cheers, Ryan " wrote: Hi All When a workbook opens, I am trying to loop through all 26 worksheets, adding up the numbers in range A1 of each sheet and displaying the result in a message box. I am failing miserably! One attempt shown below. Please let me know where I'm going wrong! Private Sub Workbook_Open() Dim WS_Count As Integer Dim I As Integer WS_Count = ActiveWorkbook.Worksheets.Count total = 0 For I = 1 To WS_Count ActiveWorkbook.Worksheets(I).Range("A1").Select nexttotal = Worksheet(I).Range("A1").Value total = total + nexttotal 'MsgBox ActiveWorkbook.Worksheets(I).Name Next I MsgBox total End Sub Many thanks in advance! Andy |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Thank you both kindly gentlemen...both work a charm! Greatly
appreciated! |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Loop worksheets | Excel Discussion (Misc queries) | |||
Naming Worksheets - Loop within a loop issue | Excel Programming | |||
Naming Worksheets - Loop within a loop issue | Excel Programming | |||
(Complex) Loop within loop to create worksheets | Excel Programming | |||
Loop Thru Worksheets | Excel Programming |