Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 60
Default FOR EACH statement

I have 3 worksheets,wanted to find the total sum of the values appeared in a
column (F say) in each of the worksheets. My following codes do not give me
the right answer. Instead it sums only the first sheet, but output triple the
total value of the FIRST sheet and ignores the other 2 sheets. Can anyone
help to resolve this. Many thanks.

Dim wkSht As Worksheet
total = 0
For Each wkSht In Worksheets

total = total + Application.Sum(Range("f8.f150"))

Next wkSht

MsgBox ("Total = ") & total
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default FOR EACH statement

You may need an activate:

Sub Macro1()
Dim w As Worksheet
t = 0
For Each w In Worksheets
w.Activate
t = t + Cells(1, 1)
Next
MsgBox (t)
End Sub

will work. There may be other problems as well

--
Gary's Student


"zhj23" wrote:

I have 3 worksheets,wanted to find the total sum of the values appeared in a
column (F say) in each of the worksheets. My following codes do not give me
the right answer. Instead it sums only the first sheet, but output triple the
total value of the FIRST sheet and ignores the other 2 sheets. Can anyone
help to resolve this. Many thanks.

Dim wkSht As Worksheet
total = 0
For Each wkSht In Worksheets

total = total + Application.Sum(Range("f8.f150"))

Next wkSht

MsgBox ("Total = ") & total

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 60
Default FOR EACH statement

Hello! Friend

Thanks for writing.

But it didn't work. Still gives triple the sum of Sheet 1 only.


zhj23

"Gary''s Student" wrote:

You may need an activate:

Sub Macro1()
Dim w As Worksheet
t = 0
For Each w In Worksheets
w.Activate
t = t + Cells(1, 1)
Next
MsgBox (t)
End Sub

will work. There may be other problems as well

--
Gary's Student


"zhj23" wrote:

I have 3 worksheets,wanted to find the total sum of the values appeared in a
column (F say) in each of the worksheets. My following codes do not give me
the right answer. Instead it sums only the first sheet, but output triple the
total value of the FIRST sheet and ignores the other 2 sheets. Can anyone
help to resolve this. Many thanks.

Dim wkSht As Worksheet
total = 0
For Each wkSht In Worksheets

total = total + Application.Sum(Range("f8.f150"))

Next wkSht

MsgBox ("Total = ") & total

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default FOR EACH statement

How about the range? I noticed it has a . rather than a :
--
Gary''s Student


"zhj23" wrote:

Hello! Friend

Thanks for writing.

But it didn't work. Still gives triple the sum of Sheet 1 only.


zhj23

"Gary''s Student" wrote:

You may need an activate:

Sub Macro1()
Dim w As Worksheet
t = 0
For Each w In Worksheets
w.Activate
t = t + Cells(1, 1)
Next
MsgBox (t)
End Sub

will work. There may be other problems as well

--
Gary's Student


"zhj23" wrote:

I have 3 worksheets,wanted to find the total sum of the values appeared in a
column (F say) in each of the worksheets. My following codes do not give me
the right answer. Instead it sums only the first sheet, but output triple the
total value of the FIRST sheet and ignores the other 2 sheets. Can anyone
help to resolve this. Many thanks.

Dim wkSht As Worksheet
total = 0
For Each wkSht In Worksheets

total = total + Application.Sum(Range("f8.f150"))

Next wkSht

MsgBox ("Total = ") & total

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default FOR EACH statement

see if this will do what you want

Sub addSheets2()
Dim shtotal As Integer
Dim total As Integer
Dim wkSht As Worksheet
Dim rng As Range

Dim i As Integer
total = 0
For Each wkSht In ThisWorkbook.Worksheets
Set rng = wkSht.Range("a8:a50")
shtotal = Application.Sum(rng)
total = shtotal + total
shtotal = 0

Next wkSht
MsgBox total

End Sub

--


Gary


"zhj23" wrote in message
...
I have 3 worksheets,wanted to find the total sum of the values appeared in
a
column (F say) in each of the worksheets. My following codes do not give
me
the right answer. Instead it sums only the first sheet, but output triple
the
total value of the FIRST sheet and ignores the other 2 sheets. Can anyone
help to resolve this. Many thanks.

Dim wkSht As Worksheet
total = 0
For Each wkSht In Worksheets

total = total + Application.Sum(Range("f8.f150"))

Next wkSht

MsgBox ("Total = ") & total





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default FOR EACH statement

you can delete the dim i line, it was left over

--


Gary


"Gary Keramidas" <GKeramidasATmsn.com wrote in message
...
see if this will do what you want

Sub addSheets2()
Dim shtotal As Integer
Dim total As Integer
Dim wkSht As Worksheet
Dim rng As Range

Dim i As Integer
total = 0
For Each wkSht In ThisWorkbook.Worksheets
Set rng = wkSht.Range("a8:a50")
shtotal = Application.Sum(rng)
total = shtotal + total
shtotal = 0

Next wkSht
MsgBox total

End Sub

--


Gary


"zhj23" wrote in message
...
I have 3 worksheets,wanted to find the total sum of the values appeared in
a
column (F say) in each of the worksheets. My following codes do not give
me
the right answer. Instead it sums only the first sheet, but output triple
the
total value of the FIRST sheet and ignores the other 2 sheets. Can anyone
help to resolve this. Many thanks.

Dim wkSht As Worksheet
total = 0
For Each wkSht In Worksheets

total = total + Application.Sum(Range("f8.f150"))

Next wkSht

MsgBox ("Total = ") & total





  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default FOR EACH statement

i adapted your code instead of using mine, in case you're interested

Sub addsheets()
Dim wkSht As Worksheet
Dim rng As Range
total = 0
For Each wkSht In Worksheets
Set rng = wkSht.Range("a8:a50")
total = total + Application.Sum(rng)
Next wkSht
MsgBox ("Total = ") & total
End Sub

--


Gary


"zhj23" wrote in message
...
I have 3 worksheets,wanted to find the total sum of the values appeared in
a
column (F say) in each of the worksheets. My following codes do not give
me
the right answer. Instead it sums only the first sheet, but output triple
the
total value of the FIRST sheet and ignores the other 2 sheets. Can anyone
help to resolve this. Many thanks.

Dim wkSht As Worksheet
total = 0
For Each wkSht In Worksheets

total = total + Application.Sum(Range("f8.f150"))

Next wkSht

MsgBox ("Total = ") & total



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 60
Default FOR EACH statement

Hi! Gary


Yes. I am very interested to know what is wrong with my codes. So when you
adopt my codes, did it work?

I tried, but it doesn't work. Still the triple of the values in Sheet 1.

Thanks anyway.


zhj23

"Gary Keramidas" wrote:

i adapted your code instead of using mine, in case you're interested

Sub addsheets()
Dim wkSht As Worksheet
Dim rng As Range
total = 0
For Each wkSht In Worksheets
Set rng = wkSht.Range("a8:a50")
total = total + Application.Sum(rng)
Next wkSht
MsgBox ("Total = ") & total
End Sub

--


Gary


"zhj23" wrote in message
...
I have 3 worksheets,wanted to find the total sum of the values appeared in
a
column (F say) in each of the worksheets. My following codes do not give
me
the right answer. Instead it sums only the first sheet, but output triple
the
total value of the FIRST sheet and ignores the other 2 sheets. Can anyone
help to resolve this. Many thanks.

Dim wkSht As Worksheet
total = 0
For Each wkSht In Worksheets

total = total + Application.Sum(Range("f8.f150"))

Next wkSht

MsgBox ("Total = ") & total




  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default FOR EACH statement

Sub addsheets4()

Dim total As Integer ' <<<<<<<<<<<<<<<<<<<<<added this
Dim rng As Range ' <<<<<<<<<<<<<<<<<<<<<<added this
Dim wkSht As Worksheet
total = 0
For Each wkSht In Worksheets
Set rng = wkSht.Range("a8:a50") ' <<<<<<<< changed this
total = total + Application.Sum(rng) ' <<<<<changed this

Next wkSht

MsgBox ("Total = ") & total

End Sub


--


Gary


"zhj23" wrote in message
...
Hi! Gary


Yes. I am very interested to know what is wrong with my codes. So when you
adopt my codes, did it work?

I tried, but it doesn't work. Still the triple of the values in Sheet 1.

Thanks anyway.


zhj23

"Gary Keramidas" wrote:

i adapted your code instead of using mine, in case you're interested

Sub addsheets()
Dim wkSht As Worksheet
Dim rng As Range
total = 0
For Each wkSht In Worksheets
Set rng = wkSht.Range("a8:a50")
total = total + Application.Sum(rng)
Next wkSht
MsgBox ("Total = ") & total
End Sub

--


Gary


"zhj23" wrote in message
...
I have 3 worksheets,wanted to find the total sum of the values appeared
in
a
column (F say) in each of the worksheets. My following codes do not
give
me
the right answer. Instead it sums only the first sheet, but output
triple
the
total value of the FIRST sheet and ignores the other 2 sheets. Can
anyone
help to resolve this. Many thanks.

Dim wkSht As Worksheet
total = 0
For Each wkSht In Worksheets

total = total + Application.Sum(Range("f8.f150"))

Next wkSht

MsgBox ("Total = ") & total






  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 60
Default FOR EACH statement

Gary

Sorry. I have done EXACTLY what you have suggested. It still doesnt work.

(Although the total is not necessary INTEGER, it does not bother me for
testing purposes)

FYI, I had made it work by using the FOR.. NEXT statement, but I want to
know why can't work with FOR EACH.

Zhj23


"Gary Keramidas" wrote:

Sub addsheets4()

Dim total As Integer ' <<<<<<<<<<<<<<<<<<<<<added this
Dim rng As Range ' <<<<<<<<<<<<<<<<<<<<<<added this
Dim wkSht As Worksheet
total = 0
For Each wkSht In Worksheets
Set rng = wkSht.Range("a8:a50") ' <<<<<<<< changed this
total = total + Application.Sum(rng) ' <<<<<changed this

Next wkSht

MsgBox ("Total = ") & total

End Sub


--


Gary


"zhj23" wrote in message
...
Hi! Gary


Yes. I am very interested to know what is wrong with my codes. So when you
adopt my codes, did it work?

I tried, but it doesn't work. Still the triple of the values in Sheet 1.

Thanks anyway.


zhj23

"Gary Keramidas" wrote:

i adapted your code instead of using mine, in case you're interested

Sub addsheets()
Dim wkSht As Worksheet
Dim rng As Range
total = 0
For Each wkSht In Worksheets
Set rng = wkSht.Range("a8:a50")
total = total + Application.Sum(rng)
Next wkSht
MsgBox ("Total = ") & total
End Sub

--


Gary


"zhj23" wrote in message
...
I have 3 worksheets,wanted to find the total sum of the values appeared
in
a
column (F say) in each of the worksheets. My following codes do not
give
me
the right answer. Instead it sums only the first sheet, but output
triple
the
total value of the FIRST sheet and ignores the other 2 sheets. Can
anyone
help to resolve this. Many thanks.

Dim wkSht As Worksheet
total = 0
For Each wkSht In Worksheets

total = total + Application.Sum(Range("f8.f150"))

Next wkSht

MsgBox ("Total = ") & total








  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default FOR EACH statement

just created a new workbook and pasted your code in
added the 2 dim statements, total and rng, and the 2 lines between the for
and next of your code

i have a total of in column a of 260 on sheet1, 70 on sheet2 and 195 in
sheet3. this totals 525 and that's what i get,.

are you sure you set this line, Set rng = wkSht.Range("a8:a50"), in the for
next loop?

are you saying that if you copy and paste this code and run it it gives you
3* the column A total of sheet1?

Sub addsheets()
Dim wkSht As Worksheet
Dim rng As Range
total = 0
For Each wkSht In Worksheets
Set rng = wkSht.Range("a8:a50")
total = total + Application.Sum(rng)
Next wkSht
MsgBox ("Total = ") & total
End Sub

--


Gary


"zhj23" wrote in message
...
Gary

Sorry. I have done EXACTLY what you have suggested. It still doesnt work.

(Although the total is not necessary INTEGER, it does not bother me for
testing purposes)

FYI, I had made it work by using the FOR.. NEXT statement, but I want to
know why can't work with FOR EACH.

Zhj23


"Gary Keramidas" wrote:

Sub addsheets4()

Dim total As Integer ' <<<<<<<<<<<<<<<<<<<<<added this
Dim rng As Range ' <<<<<<<<<<<<<<<<<<<<<<added this
Dim wkSht As Worksheet
total = 0
For Each wkSht In Worksheets
Set rng = wkSht.Range("a8:a50") ' <<<<<<<< changed this
total = total + Application.Sum(rng) ' <<<<<changed this

Next wkSht

MsgBox ("Total = ") & total

End Sub


--


Gary


"zhj23" wrote in message
...
Hi! Gary


Yes. I am very interested to know what is wrong with my codes. So when
you
adopt my codes, did it work?

I tried, but it doesn't work. Still the triple of the values in Sheet
1.

Thanks anyway.


zhj23

"Gary Keramidas" wrote:

i adapted your code instead of using mine, in case you're interested

Sub addsheets()
Dim wkSht As Worksheet
Dim rng As Range
total = 0
For Each wkSht In Worksheets
Set rng = wkSht.Range("a8:a50")
total = total + Application.Sum(rng)
Next wkSht
MsgBox ("Total = ") & total
End Sub

--


Gary


"zhj23" wrote in message
...
I have 3 worksheets,wanted to find the total sum of the values
appeared
in
a
column (F say) in each of the worksheets. My following codes do not
give
me
the right answer. Instead it sums only the first sheet, but output
triple
the
total value of the FIRST sheet and ignores the other 2 sheets. Can
anyone
help to resolve this. Many thanks.

Dim wkSht As Worksheet
total = 0
For Each wkSht In Worksheets

total = total + Application.Sum(Range("f8.f150"))

Next wkSht

MsgBox ("Total = ") & total








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
IF statement inside a SUMIF statement.... or alternative method Sungibungi Excel Worksheet Functions 3 December 4th 09 06:22 PM
Reconcile Bank statement & Credit card statement & accounting data Bklynhyc Excel Worksheet Functions 0 October 7th 09 09:07 PM
Embedding an OR statement in an IF statement efficiently Chatnoir11 Excel Discussion (Misc queries) 4 February 2nd 09 08:12 PM
appending and IF statement to an existing IF statement spence Excel Worksheet Functions 1 February 28th 06 11:00 PM
Help please, IF statement/SUMIF statement Brad_A Excel Worksheet Functions 23 January 11th 05 02:24 PM


All times are GMT +1. The time now is 08:38 AM.

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"