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
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
I guess I didn't made myself very clear. I want my string to be of max length 58 and that also from right to left.
Below is the just the example where I want strings to be trimmed to 5 characters from right to left.

StringsResult
abc.comc.com
sdafadfasdfadsfsd.comd.com
dsfasdfjsadlfksadf@gmail.coml.com
sfasdfadsfasd@fdasdf.comf.com
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
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?
You didn't answer that question so I have assumed b) as I did before.

VBA Code:
Sub CutOff_v2()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate("right(" & .Address & ",5)")
  End With
End Sub


If you did want a) then this change would put the results in the next column beside the original.
Rich (BB code):
.Value = Evaluate("right(" & .Address & ",5)")
.Offset(, 1).Value = Evaluate("right(" & .Address & ",5)")
 
Upvote 0

Forum statistics

Threads
1,214,615
Messages
6,120,538
Members
448,970
Latest member
kennimack

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