在excel中截断数据(删除前12个字符)

这是我迄今为止:

Sub Truncate() Application.ScreenUpdating = False Dim LastHeader As Long Dim MyCell As Range LastHeader = Range("IV1").End(xlToLeft).Column For Each MyCell In Application.Intersect(Sheet6.UsedRange, Columns("I")) MyCell.Value = Right(MyCell.Value, Len(MyCell.Value) - 12) Next MyCell Application.ScreenUpdating = True End Sub 

我想让这个macros删除“I”列中的前12个字符,而剩下的就是它。 当我没有使用(Len(MyCell.Value) – 12)时,我得到了这个macros为其他列工作,而只是input一个像Right(MyCell.Value,1),它保留在右边的第一个字母只要。 但是对于这一列,数据右边的数字量是不一样的,所以最好只删除数据左边的指定数量的字符。

哦,顺便说一句,我得到的错误是run-time error '5'

谢谢你的帮助 :)

几乎可以肯定其中一个条目有12个或更less的字符,所以你已经结束了调用Right作为第二个参数0或负数。

如果您想让单元格空白,如果有12个或更less的字符,请尝试:

 If (Len(MyCell.Value) > 12) Then MyCell.Value = Right(MyCell.Value, Len(MyCell.Value) - 12) Else MyCell.Value = "" End If 
Interesting Posts