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.
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
Excel Workbook | |||||
---|---|---|---|---|---|
H | I | J | |||
1 | H54321 | I | J | ||
2 | HHH54321 | I | J | ||
3 | HH54321 | I | J | ||
4 | H54321 | I | J | ||
5 | HHHH54321 | I | J | ||
6 | HHH54321 | I | J | ||
7 | H | I | J | ||
8 | HH | I | J | ||
9 | HHH | I | J | ||
10 | HHHH | I | J | ||
11 | HHHHH | I | J | ||
12 | HHHHHH | I | J | ||
13 | H54321 | I | J | ||
14 | |||||
Sheet1 |
Excel Workbook | |||||
---|---|---|---|---|---|
H | I | J | |||
1 | H | I | J | ||
2 | HHH | I | J | ||
3 | HH | I | J | ||
4 | H | I | J | ||
5 | HHHH | I | J | ||
6 | HHH | I | J | ||
7 | H | I | J | ||
8 | HH | I | J | ||
9 | HHH | I | J | ||
10 | HHHH | I | J | ||
11 | HHHHH | I | J | ||
12 | H | I | J | ||
13 | H | I | J | ||
14 | |||||
Sheet1 |
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