String Slicing (How to)

DaveUK

Board Regular
Joined
Jan 24, 2005
Messages
245
I have a range of undefined length (rows) in Column A of worksheet "Data".

What i want to do is use the left$ type function so all text in each row in the range (Column A) is restricted to the first 24 characters only.

Please could someone advise how to do this.

Many thanks in advance.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Have you looked at Data>Text to Columns... using Fixed Width.
 
Upvote 0
=left(A1,24)

Just copy that formula down the entire column of data.

HTH
Cal
 
Upvote 0
In VBA:
sub myLen24()
'Define the length on the rows assume data is in column a cell2 and down
Set myRange = Range(Range("A2"), Range("A2").End(xlDown))
'Set the length as desired in column b
'you may want to insert a column here if there is already data in column b
myRange.Offset(0, 1).FormulaR1C1 = "=(LEFT(RC[-1],24))"
end sub

Then if desired, copy column b and paste sepcial values into column a and delete column b
 
Upvote 0
If you want VBA then something like this might work:

Code:
Sub Make24
Dim rng As Range
Dim c As Range
Dim LastRow

     LastRow = Range("A65536").End(xlUp).Row

     Set rng = Range("A1:A" & LastRow)

     For Each c in rng.Cells
          c.Value = Left(c.Value, 24)
     Next c

End Sub

If there are lots of entries in column A this could be quite slow.
 
Upvote 0
Oh Yeah, :eek: :biggrin:

Sub TrimCell()
Dim cell as range
For each cell in range("A1",range("A65535").end(xlup))
cell.value = left(Cell.value,24)
Next
End sub

This will trim the entire range of cell to the 24 characters, no cutting or pasting.

Still like Norie's idea though.

HTH
Cal

One upsmenship at it's best :devilish: Of course Norie beat me to it.
 
Upvote 0

Forum statistics

Threads
1,214,901
Messages
6,122,157
Members
449,068
Latest member
shiz11713

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