Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

Problem with WorksheetFunction in Worksheet Module

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
Massimo Scola
Asked:
Massimo Scola
  • 6
  • 3
1 Solution
 
Martin LissRetired ProgrammerCommented:
Change line 12 to

Public Sub UpdateValues()
0
 
Massimo ScolaAuthor Commented:
I tried it with Public Sub but it doesn't work.
0
 
Martin LissRetired ProgrammerCommented:
What do you mean when you say "it doesn't work"? Do you get an error? Does nothing happen?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Martin LissRetired ProgrammerCommented:
Is the problem that you always get the "There was a problem message"? If so add Exit Sub following line 37.
0
 
Massimo ScolaAuthor Commented:
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
 
Martin LissRetired ProgrammerCommented:
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
 
Martin LissRetired ProgrammerCommented:
Did my last post help you?
0
 
Massimo ScolaAuthor Commented:
Hello Martin

Yes, it worked very well.

Thanks a lot

Massimo
0
 
Martin LissRetired ProgrammerCommented:
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now