Urgent:Trim, Len functions does not work well in VBA Code

krisrajz

New Member
Joined
Jun 12, 2004
Messages
2
Why do functions like Trim, Len etc., does not work in VBA modules?
Referece list were still valid.

Thank you.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Re: Urgent:Trim, Len functions does not work well in VBA Cod

They do work in VBA.
Can you please post a sample of what is giving you problems?
 
Upvote 0
Re: Urgent:Trim, Len functions does not work well in VBA Cod

Call to those functions triggers invalid argument error.But I've checked the arguments (for datatype, value etc). There was nothing wrong.

RAJ
 
Upvote 0
Re: Urgent:Trim, Len functions does not work well in VBA Cod

Someone might be able to help you if you post the code that is giving you trouble and maybe an explanation of what you are trying to do.
 
Upvote 0
Re: Urgent:Trim, Len functions does not work well in VBA Cod

Hi as noted its hard to tell you what your doing wrong unless you post your code. See the VBA Help topic 'Using Microsoft Excel Worksheet Functions in Visual Basic' reagrding using functions.

Briefly, there are 2 types of functions available to you in code - those that are native to VBA and some Worksheet functions - see the VBA help topic 'List of Worksheet Functions Available to Visual Basic'.

As an example, if you look in the help under the worksheet functions available you will see that Len is not available while Trim is. Therefore if you try and use the Len worksheet function it will fail. However, if there is no worksheet function it often is because there is an equivalent native VBA function that does the same thing. Look in VBA help for the word Len and you will see there is a Len function. Sometimes, Excel has both - there is a worksheet function Trim as well as a VBA function Trim.

Heres an example using these versions. Note that when you refer to a cell reference you need to use Range rather than just the address...

Code:
Sub TestFunctions()
Dim WF_LEN As Integer, VBA_LEN As Integer
Dim WF_TRIM As String, VBA_TRIM As String
WF_LEN = Application.WorksheetFunction.Len(Range("a1")) 'This will produce an error saying this is not supported
VBA_LEN = Len(Range("b1")) 'this will work
MsgBox VBA_LEN
WF_TRIM = Application.WorksheetFunction.Trim(Range("a1")) 'this will work
VBA_TRIM = Trim(Range("b1")) 'this will work
Range("a1").Value = WF_TRIM
Range("b1").Value = VBA_TRIM

End Sub
 
Upvote 0
I'm having a similar problem

worksheetfunction.LEN is unavailable in my module.

worksheetfunction.SUM works

worksheetfunction.COUNT works

another example:

Sub LEN()
msgbox worksheetfunction.Len(ActiveCell)
End Sub

this wont work. It doesn't even list LEN as a function in VBA.
 
Upvote 0
Len is a native VBA function.

Code:
MsgBox Len(ActiveCell.Value)
 
Upvote 0

Forum statistics

Threads
1,220,967
Messages
6,157,131
Members
451,399
Latest member
alchavar

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