Using TRIM with variable length fields

Maggielove

New Member
Joined
Apr 3, 2018
Messages
11
Is it possible to use TRIM where the field lengths are not consistent? I need to remove the first character of a field but the length varies between 4 and 8 characters, so wondered what the options were please.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
72,989
Office Version
  1. 365
Platform
  1. Windows
How about
=REPLACE(A1,1,1,"")
 

Maggielove

New Member
Joined
Apr 3, 2018
Messages
11
Thanks for the reply. I did wonder about REPLACE, but I wasn’t sure if it would give me a blank text space in what would be a numeric field after the character was removed.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
72,989
Office Version
  1. 365
Platform
  1. Windows
It wont give a blank space, but it will return a string rather than a number. To coerce the value into a number, use
=--REPLACE(A1,1,1,"")
 

Maggielove

New Member
Joined
Apr 3, 2018
Messages
11

ADVERTISEMENT

That’s great. Many thanks for your help ?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
72,989
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows

ADVERTISEMENT

You could also use MID:
=--MID(A1,2,8)
 

Maggielove

New Member
Joined
Apr 3, 2018
Messages
11
Thanks for the reply. I am still not getting the results I need so I wondered if the range definition is something to do with it.
This code works, but only on fixed length, so how can I amend to include variable length of characters please?

Dim MyTrim As Range
Dim MyTop As Range
Set MyTrim = Range(“d2:d2001”)
For Each MyTop In MyTrim
MyTop = “=RIGHT(RC[1],4)”
Next MyTop
 

tyija1995

Well-known Member
Joined
Feb 26, 2019
Messages
781
Office Version
  1. 365
Platform
  1. Windows
Try put MyTop = MID(MyTop, 2)
This will set MyTop to equal the 2nd character up until the end character, as when you don't specifiy number of chars in the MID function it will go to the end of the string (in VBA not Excel)
 
Last edited:

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,713
Office Version
  1. 365
Platform
  1. Windows
That is going to produce a specific number of characters because you ask it to produce a specific number of characters, ie 4. It is also very different to what you asked for in the first place and ignores the replies you had. You also dont need to use a loop to place a formula into a range of cells.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,478
Messages
5,831,913
Members
430,091
Latest member
Generally_confused

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