Solved

Problem with WorksheetFunction in Worksheet Module

Posted on 2013-06-11
9
214 Views
Last Modified: 2013-06-19
Hi

I noticed that two of my WorksheetFunction formulae do not work if they are in a worksheet module. Is there a reason why I cannot create code with WorksheetFunction in such modules or is it a bug?

Massimo

Option Explicit


Private Sub Worksheet_Activate()
UpdateValues
'UpdateStatisticsValues
End Sub




Sub UpdateValues()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Statistics")

Dim shpTotalOrders As ShapeRange
Dim shpTotalCustomers As ShapeRange

Set shpTotalOrders = ws.Shapes.Range(Array("TotalOrders"))
Set shpTotalCustomers = ws.Shapes.Range(Array("TotalCustomers"))

Dim lngOrders As Long
Dim intCustomers As Integer

'Doesn't work?
On Error GoTo Hell

lngOrders = Application.WorksheetFunction.Count(Range("Orders[ID]"))
intCustomers = Application.WorksheetFunction.CountIf(Range("Customers[Orders]"), ">0")

shpTotalOrders.TextFrame2.TextRange.Characters.Text = lngOrders
shpTotalCustomers.TextFrame2.TextRange.Characters.Text = intCustomers

Set ws = Nothing
Set shpTotalOrders = Nothing
Set shpTotalCustomers = Nothing

Hell:

MsgBox "There was a problem."

End Sub

Open in new window

ExampleWorksheet.xlsm
0
Comment
Question by:Massimo Scola
  • 6
  • 3
9 Comments
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39238116
Change line 12 to

Public Sub UpdateValues()
0
 

Author Comment

by:Massimo Scola
ID: 39238156
I tried it with Public Sub but it doesn't work.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39238167
What do you mean when you say "it doesn't work"? Do you get an error? Does nothing happen?
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 46

Expert Comment

by:Martin Liss
ID: 39238190
Is the problem that you always get the "There was a problem message"? If so add Exit Sub following line 37.
0
 

Author Comment

by:Massimo Scola
ID: 39241646
I get a run-time error 1004: "Method 'Range' of of Object _Worksheet failed.

I don't get any error messages of the code is put in a regular module.

Do I need to change something on my code or does it work properly on your computer?

error message
0
 
LVL 46

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 39241733
Try this. Change lines 28 and 29 to

With ws
    lngOrders = Application.WorksheetFunction.Count(.Range("Orders[ID]"))
    intCustomers = Application.WorksheetFunction.CountIf(.Range("Customers[Orders]"), ">0")
End With

Open in new window


Note that there is a dot before "Range" in both lines.

If that doesn't work, please post your workbook and list the steps to take to reproduce the error.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39251466
Did my last post help you?
0
 

Author Closing Comment

by:Massimo Scola
ID: 39259482
Hello Martin

Yes, it worked very well.

Thanks a lot

Massimo
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39259613
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question