Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default VBA Countif across sheets?


Hello! I'm running into a problem using the the Countif function in a VBA code. If I type =countapples() into cell A1 of Sheet1, it returns the expected answer. See countapples() function below:

________________________________________________
Function CountApples()

Dim TopOfRange As Double
Dim BottomOfRange As Double

TopOfRange = 4
BottomOfRange = 10

Set SearchRange = Worksheets("Sheet1").Range(Cells(TopOfRange, 1), Cells(BottomOfRange, 100))

ReturnCount = Application.WorksheetFunction.CountIf(SearchRange, "apple")

CountApples = ReturnCount

End Function
________________________________________________


BUT if I type =CountApples() into cell A1 of Sheet2, it returns #VALUE!

What's the problem with this code? Can CountIf not work across sheets?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default VBA Countif across sheets?

Hello! I'm running into a problem using the the Countif function in
a VBA code. If I type =countapples() into cell A1 of Sheet1, it
returns the expected answer. See countapples() function below:

________________________________________________
Function CountApples()

Dim TopOfRange As Double
Dim BottomOfRange As Double

TopOfRange = 4
BottomOfRange = 10

Set SearchRange = Worksheets("Sheet1").Range(Cells(TopOfRange, 1),
Cells(BottomOfRange, 100))

ReturnCount = Application.WorksheetFunction.CountIf(SearchRange,
"apple")

CountApples = ReturnCount

End Function
________________________________________________


BUT if I type =CountApples() into cell A1 of Sheet2, it returns
#VALUE!

What's the problem with this code? Can CountIf not work across
sheets?


The object ref is not fully qualified to "Sheet1" using 'Cells' as is.
Try...


Public Function CountApples#()
Dim rngSearch As Range

Const dTopRow# = 4: Const dLastRow# = 10

Application.Volatile
With Sheets("Sheet1")
Set rngSearch = .Range(.Cells(dTopRow, 1), .Cells(dLastRow, 100))
End With
CountApples = WorksheetFunction.CountIf(rngSearch, "apple")
Set rngSearch = Nothing
End Function

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default VBA Countif across sheets?

Hi again,

Am Wed, 31 Dec 2014 09:19:00 +0100 schrieb Claus Busch:

Set wsh = Application.Caller.Parent


please ignore my previous post. I misunderstood your problem.


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default VBA Countif across sheets?

More appropriately, row/col counts are Type Long, as well as the return
from CountIf. Using Type Double suggests you expect a decimal value,but
only whole numbers are used/returned...


Public Function CountApples&()
Dim rngSearch As Range

Const dTopRow& = 4: Const dLastRow& = 10

Application.Volatile
With Sheets("Sheet1")
Set rngSearch = .Range(.Cells(dTopRow, 1), .Cells(dLastRow, 100))
End With
CountApples = WorksheetFunction.CountIf(rngSearch, "apple")
Set rngSearch = Nothing
End Function

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default VBA Countif across sheets?

Thank you!
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
COUNTIF, Sorting, on Two Sheets RJB Excel Discussion (Misc queries) 1 April 7th 10 05:08 AM
Countif looking at many sheets Jock Excel Worksheet Functions 7 October 1st 09 05:13 PM
COUNTIF for multiple sheets Keyrookie Excel Worksheet Functions 3 January 20th 09 10:43 PM
Using COUNTIF across different sheets JStiehl Excel Discussion (Misc queries) 1 May 14th 08 02:17 PM
COUNTIF across sheets MaggieMagill Excel Worksheet Functions 2 October 7th 05 08:18 AM


All times are GMT +1. The time now is 01:37 AM.

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

About Us

"It's about Microsoft Excel"