Using Excel functions in a module - To Application.WorksheetFunction or not

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,480
I am building a tool that will need distributing to a number of users and there is the TRIM function being used in a module.

Now I have run into issues before where a tool wasn't being distributed and was only used by one person and different users have required the use of either of the following prior to the function
  • Application.WorksheetFunction
  • Application
  • WorksheetFunction
How do I establish which of these will be required or is there a catch all option?
 

Some videos you may like

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,114
Office Version
  1. 2016
Platform
  1. Windows
Re: Using Excel functions in a module - To Application.WorjsheetFunction or not

The post is not clear to me
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,909
Re: Using Excel functions in a module - To Application.WorjsheetFunction or not

TRIM is very unlikely to error. I would use the syntax WorksheetFunction.Trim("some string")

I think what you are referring to is that WorksheetFunction.Match will cause a VBA run-time error if there is no match, while Application.Match will return a CVErr value when there is no match.

With Match (and HLookup, VLookup, etc.) I would use the Application.Match syntax so the result can be tested with IsNumeric or IsError rather than having to write a VB error handler.

But TRIM is very unlikely to error so WorksheetFunction.Trim would be my preference.
 

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,480
Re: Using Excel functions in a module - To Application.WorjsheetFunction or not

Thanks Mikerickson, I think that has answered my question.
 

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,480

ADVERTISEMENT

Re: Using Excel functions in a module - To Application.WorjsheetFunction or not

I am now experiencing this issue with the following
Code:
currMinAxis = WorksheetFunction.Min(Sheets("Daily Report").Range("DailyReport_AllAvgILS")) - 0.15
This is working as expected when I am using the file but my client is experiencing a 'Run-time error '1004': Unable to get the Min property of the WorksheetFunction class'

I am at a loss as to how this should work and what combination of Application and WorksheetFunction I should use :(
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,909
Re: Using Excel functions in a module - To Application.WorjsheetFunction or not

Try qualifying the range even more fully.

Workbooks("Workbook1.xlsm").Sheets("Daily Report").Range("DailyReport_AllAvgILS")

or even
Workbooks("Workbook2.xlsm").Names("DailyReport_AllAvgILS").RefersToRange

and switching to Application.Min wouldn't hurt.
 

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,480

ADVERTISEMENT

Re: Using Excel functions in a module - To Application.WorjsheetFunction or not

Thanks, I'll give that a go.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,068
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Re: Using Excel functions in a module - To Application.WorjsheetFunction or not

You should only get that error if there are error values in the range in question.
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,909
Re: Using Excel functions in a module - To Application.WorjsheetFunction or not

It looks like another situation where WorksheetFunction causes a run-time error, but Application returns an error value.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,068
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Re: Using Excel functions in a module - To Application.WorjsheetFunction or not

That's always the case, as far as I am aware.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,599
Messages
5,597,105
Members
414,125
Latest member
iQQ

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top