How to Subtitute the Strings in a Range if Length Exceeds

ygoyal578

New Member
Joined
Apr 26, 2020
Messages
31
Office Version
  1. 2016
Platform
  1. Windows
I'm looking for the code where

If Length of any cell exceeds defined max length then string in that cell gets substituted max length from left.
And, if length of cell is within range then skip that cell and go to next cell.

All this is to be done in same cell and column.

Below is the example:

Column A is with string and Column B is Length(just to show). Max length of cell to be set at 5


and output required is below
 

Attachments

  • 1590831482328.png
    1590831482328.png
    6.6 KB · Views: 8
  • 1590831531174.png
    1590831531174.png
    4.7 KB · Views: 10

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Just use =LEFT(A2,5)

Anything shorter than 5 characters will just show whatever is in the cell.
 
Upvote 0
All this is to be done in same cell and column.
Try this with a copy of your data.

VBA Code:
Sub CutOff()
  Range("A2", Range("A" & Rows.Count).End(xlUp)).TextToColumns DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(5, 9))
End Sub
 
Upvote 0
Hello Jason, Actually I need the macro code for doing it.
So did the suggested macro do what you wanted?
(BTW, if you want a specific method - eg macro - it would save people wasted effort if that was stated clearly in your first post. ;))
 
Upvote 0
So did the suggested macro do what you wanted?
(BTW, if you want a specific method - eg macro - it would save people wasted effort if that was stated clearly in your first post. ;))
He Peter, Thanks for the help, I have tried the code and was running fine but I want the dynamic formula where length of string remains to 58 and if length is greater than 58 then extra length should be substituted from left to right. Currently it substituting from right to left.

Or we can also have the code where it by default make the string for defined length from right to left
 
Upvote 0
All this is to be done in same cell and column.
but I want the dynamic formula
Perhaps I am not understanding but these seem contradictory to me. If you want a formula, it cannot go in the cell the data is already in. So, do you want ..
a) a formula producing the results in another column, or
b) code that replaces the original data with the shortened values?

I am also confused about the right/left. My code reproduced the results you gave as examples in post #1. Are you now saying those samples were incorrect and the values should be truncated at the other end?

Another set of sample data and expected results may help clarify. However, please don't just post an image as we cannot copy from that. Preferred is to use XL2BB but otherwise, just copy from your sheet and paste in the forum like this

StringsResult
abc.comabc.c
dedc.comdedc.
lkjuhygtlkjuh
juhytfrjuhyt
2lokij2loki
sdertsdert
dfgtdfgt
sgdsgd
ujhytgfrujhyt
 
Upvote 0

Forum statistics

Threads
1,215,352
Messages
6,124,449
Members
449,160
Latest member
nikijon

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