Using TRIM in VBA

JanetW

New Member
Joined
May 12, 2016
Messages
21
Hi All

I am trying to use the TRIM function in VBA but am struggling!

The code which works in excel is:

=TRIM(RIGHT(SUBSTITUTE(N2,"-",REPT(" ",100)),100))

when I try to use the same in VBA, I get a compile error: expected end of statement

I assume that this is because of the " within the code (so VBA thinks code is finished?) so I tried to get round this by creating the formula as a string as follows:

SupplierNoFormula = "=TRIM(RIGHT(SUBSTITUTE(N2," & """ & "-" & """ & ",REPT(" & """ & " " & """ & ",100)),100))" - which also doesn't work!!!

what I basically have is a list in one column of supplier name and number (separated by '-' ie A SUPPLIER-OR12_12345) and I need to extract the supplier number (OR12_12345) to enable me to do a lookup to another report

Any help would be greatly appreciated

Thanks in advance!

Janet
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi try
Range("A1").Formula = "=TRIM(RIGHT(SUBSTITUTE(N2,""-"",REPT("" "",100)),100))"
 
Upvote 0
You can use something like this in code:

Code:
myCell = Range("N2").Value
myVal = Trim(Mid(myCell, InStrRev(myCell, "-") + 1, Len(myCell)))
 
Upvote 0
You can use something like this in code:

Code:
myCell = Range("N2").Value
myVal = Trim(Mid(myCell, InStrRev(myCell, "-") + 1[COLOR="#FF0000"][B], Len(myCell)[/B][/COLOR]))
I think the OP's question is really about how to deal with the quotes in vba but I have a comment on your code anyway.
Since the vba Mid() function does not require the 3rd (length) argument, you could just omit the red part & save that calculation.
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
Thank you so much for the quick reply, I have used Fluff's solution but appreciate the response and will keep this for possible future use!

You can use something like this in code:

Code:
myCell = Range("N2").Value
myVal = Trim(Mid(myCell, InStrRev(myCell, "-") + 1, Len(myCell)))
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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