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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,318
Office Version
  1. 365
Platform
  1. Windows
Have you looked at Data>Text to Columns... using Fixed Width.
 

Cbrine

Well-known Member
Joined
Dec 2, 2003
Messages
3,196
=left(A1,24)

Just copy that formula down the entire column of data.

HTH
Cal
 

SteveD

Board Regular
Joined
Feb 20, 2002
Messages
104
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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,318
Office Version
  1. 365
Platform
  1. Windows
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.
 

Cbrine

Well-known Member
Joined
Dec 2, 2003
Messages
3,196
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.
 

Forum statistics

Threads
1,147,691
Messages
5,742,661
Members
423,746
Latest member
Joaogomes

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