![](/img/trans.png)
[英]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.