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.
 

Some videos you may like

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,210
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
46,210
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
46,210
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,955
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
766
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,110
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,108,814
Messages
5,525,050
Members
409,617
Latest member
Lenaf

This Week's Hot Topics

Top