Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Loop through worksheets

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Loop through worksheets

Sometimes, you call your accumulator Total. Sometimes, you call it nexttotal.

I bet that's enough...

But you don't need to select the worksheet:

Private Sub Workbook_Open()

Dim WS_Count As Long
Dim I As long
dim Total as double

WS_Count = me.Worksheets.Count

For I = 1 To WS_Count
if isnumeric(me.worksheets(I).range("A1").value) then
total = total+ me.worksheets(i).range("a1").value
end if
next i

MsgBox total

End Sub

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


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 586
Default Loop through worksheets

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Loop through worksheets

Thank you both kindly gentlemen...both work a charm! Greatly
appreciated!

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
Loop worksheets merry_fay Excel Discussion (Misc queries) 2 January 7th 09 01:54 PM
Naming Worksheets - Loop within a loop issue klysell Excel Programming 5 March 29th 07 05:48 AM
Naming Worksheets - Loop within a loop issue klysell Excel Programming 0 March 27th 07 11:17 PM
(Complex) Loop within loop to create worksheets klysell Excel Programming 1 March 20th 07 12:03 AM
Loop Thru Worksheets Tinkerbell[_2_] Excel Programming 2 January 30th 07 10:13 PM


All times are GMT +1. The time now is 12:47 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"