[英]How to remove the last character of a word in a text string and insert to another cell using VBA in Excel?
[英]How to i remove a text after '*' or '-' character using VBA in excel?
我编写了以下代码,在excel中使用VBA但在其给出错误的行中的'*'或' - '字符后删除文本。 有人可以帮忙吗?
Sub Removetext()
For each c In Range("A1:ZZ1")
c.Value = Left(c.Value, InStr(c.Value, "-") - 1)
Next C
End Sub
正如@JNevill和@fidnwindow的评论中所述,您需要测试是否找到了搜索对象:
Sub Removetext()
For Each c In Range("A1:ZZ1")
If InStr(c.Value, "-") > 0 Then
c.Value = Left(c.Value, InStr(c.Value, "-") - 1)
End If
If InStr(c.Value, "*") > 0 Then
c.Value = Left(c.Value, InStr(c.Value, "*") - 1)
End If
Next c
End Sub
问题是,当InStr找不到标准时,它返回0
。 所以现在你正在寻找将抛出和错误的左-1个字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.