Excel delete last 5 characters

Robertson1995

Board Regular
Joined
Apr 1, 2009
Messages
121
Hey guys, I need code that will delete the last 5 characters of every cell in Column H. Thanks in advance for the help.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
An array code is one option

Another way would be a simple IF test in a working column (manual or via VBA) that would then post the truncated strings back over column H

hth

Dave

Code:
Sub ArrayWrite()
    Dim rng1 As Range
    Dim X
    Dim lngRow As Long
    Set rng1 = Range([h1], Cells(Rows.Count, "H").End(xlUp))
    X = rng1
    For lngRow = 1 To UBound(X)
        If Len(X(lngRow, 1)) >= 5 Then X(lngRow, 1) = Right$(X(lngRow, 1), Len(X(lngRow, 1)) - 5)
    Next
    rng1 = X
End Sub
 
Upvote 0
Robertson1995,


Sample data before the macro:


Excel Workbook
HIJ
1H54321IJ
2HHH54321IJ
3HH54321IJ
4H54321IJ
5HHHH54321IJ
6HHH54321IJ
7HIJ
8HHIJ
9HHHIJ
10HHHHIJ
11HHHHHIJ
12HHHHHHIJ
13H54321IJ
14
Sheet1





After the macro:


Excel Workbook
HIJ
1HIJ
2HHHIJ
3HHIJ
4HIJ
5HHHHIJ
6HHHIJ
7HIJ
8HHIJ
9HHHIJ
10HHHHIJ
11HHHHHIJ
12HIJ
13HIJ
14
Sheet1





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub DeleteLast5()
' hiker95, 04/19/2011
' http://www.mrexcel.com/forum/showthread.php?t=544815
Dim LR As Long, LC As Long
Application.ScreenUpdating = False
LR = Cells(Rows.Count, "H").End(xlUp).Row
LC = Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
Cells(1, LC + 2).Formula = "=IF(LEN(H1)<6,H1,LEFT(H1,LEN(H1)-5))"
Cells(1, LC + 2).AutoFill Destination:=Range(Cells(1, LC + 2), Cells(LR, LC + 2))
Columns("H:H").Value = Columns(LC + 2).Value
Columns(LC + 2).ClearContents
Application.ScreenUpdating = True
End Sub


Then run the DeleteLast5 macro.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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