Simply... why isnt this TRIM() trimming please?

Grizlore

Active Member
Joined
Aug 22, 2006
Messages
259
Hi all,

I am having a stupid day... and can't see why this isn't working...

Simply... why isnt this TRIM() trimming please?

Code:
Private Sub CommandButton1_Click()

        Dim sapFARM As String
        
        
        sapFARM = Trim("http://maps.google.co.uk/maps?f=d&source=s_d&saddr=SW1A1AA&daddr=" & txtPostcode.Value)

MsgBox sapFARM ' just to check visually if trimmed
     
        ActiveWorkbook.FollowHyperlink Trim(sapFARM), NewWindow:=True

End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Is this trying to TRIM the space from the postcode?

You'll need to use something like REPLACE as TRIM will only remove extra spaces - if there's only 1 space it will leave it in.
 
Upvote 0
VBA Trim will only trim leading and trailing spaces so it works slightly differently to the worksheet function version of TRIM.

What value are you actually passing to it (ie what value does txtPostCode contain) and what result do you want to achieve?
 
Upvote 0
D'oh

Silly me.. told you I was having one of those days!

Thanks, ignore me


Thanks for your time guys !

edit - Yes... I wanted all the spaces gone... so I shall replace them. I was using TRIM in error !
 
Last edited:
Upvote 0
my guess:
since the first part has nothing to trim you probably only need to trim the secon one
so try:
sapFARM = "http://maps.google.co.uk/maps?f=d&source=s_d&saddr=SW1A1AA&daddr=" & trim(txtPostcode.Value)
 
Upvote 0
VBA Trim will only trim leading and trailing spaces so it works slightly differently to the worksheet function version of TRIM.

I stand corrected.

More importantly.... why do you need directions from Buckingham Palace? :)
 
Upvote 0
The Trim worksheet function works differently from the Trim in vba; They both remove all leading and trailing spaces, but the worksheet one additionally reduces to one space, any multiple spaces between words in a string.
To remove spaces altogether, use Substitute on a worksheet, or replace in vba:
x=substitute(A1," ","")
x=replace(oldstring," ","")

edit: goodness! the time to take a slurp of coffee while answering and an avalanche of answers..
 
Last edited:
Upvote 0
@p45cal

Thanks, I used your REPLACE suggestion

Code:
trimmedPCode = Replace(txtPostcode.Value, " ", "")

Thansk to everyone else who kindly gave suggestions
 
Upvote 0

Forum statistics

Threads
1,214,632
Messages
6,120,649
Members
448,975
Latest member
sweeberry

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