Why is TRIM function failing in VBA?

ez08mba

Board Regular
Joined
Nov 1, 2011
Messages
224
Office Version
  1. 2016
Platform
  1. Windows
We have a macro for users to run. One user has Excel 2007. Would this version be the reason the following code fails on the "TRIM" function? I works in 2010 and later.

Code:
theAnswer = UCase(Trim(Cells(ques, 2).Text))

or

Code:
theCaption = UCase(Trim(UserForm1.Choice1.Caption)) ' This is reading a RadioButton caption
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
In what way doesn't it work?
 
Upvote 0
If you're getting a "Can't find project or library" error, you have a missing reference. If it's a different error, tell us what. :)
 
Upvote 0
Try:
Code:
theAnswer = UCase([COLOR=#ff0000]WorksheetFunction.[/COLOR]Trim(Cells(ques, 2).Text))

or

Code:
theCaption = UCase([COLOR=#FF0000]WorksheetFunction.[/COLOR]Trim(UserForm1.Choice1.Caption))

or

Code:
theCaption = UCase([COLOR=#FF0000]WorksheetFunction.[/COLOR]Trim(UserForm1.[COLOR=#ff0000]OptionButton1[/COLOR].Caption))
 
Upvote 0
Fluff, RoryA and DanteAmor!

Sorry for the delay. I've been trying to get a screen shot from the user but it is possible they may not respond. However, on the phone I do recall them mentioning the "Can't find project library." I will try these suggestions and respond to whether I was able to resolve their issue or not.

This does prompt another question. We don't write a lot of Excel macros for customers, so is this call to "Trim" as I have posted incorrect and should we be referencing it differently in later versions of Excel (e.g. 2010 - 2019)?

Thanks for the prompt responses!
 
Upvote 0
You are using Trim correctly & it hasn't changed.
As Rorya mentioned you are probably missing a reference. In the VB Editor select Tools > References. Is there anything labeled as "MISSING"
 
Upvote 0
Note: you'll need to get the user to check the references if the code is working on your machine.
 
Upvote 0
Also be aware that the vba Trim() function does not do the same job as the worksheet Trim() function. vba Trim() function only trims leading and trailing spaces, it does not contract multiple internal spaces down to one like the worksheet function does.
 
Upvote 0
You are using Trim correctly & it hasn't changed.
As Rorya mentioned you are probably missing a reference. In the VB Editor select Tools > References. Is there anything labeled as "MISSING"


I don't see anything labeled "MISSING" in my references, but there may be on user's machine as their security is locked down tight on their machines due to corporate policies - meaning I may not be able to correct it.

Peter - I will note that. We are only looking to trim trailing spaces on the ends.
 
Upvote 0
What references do you have set in that workbook, and are you using the same version of Windows/Office as the user?
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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
Back
Top