我有一个像这样的单元格值:
This is a line. /delete words in the area /keep this part
假设单元格为A1
,单元格值包含两个/
s。我想使用excel VBA删除它们之间的字母,并将单元格值更改为:
This is a line. keep this part
其实我之前问过类似的问题:Excel VBA: How to remove substrings from a cell?
我有两个不同的符号来定位子串,但在这里我有两个相同的符号。然后我不知道如何重写代码。
将提供任何帮助。谢谢!
答案 0 :(得分:3)
您没有提及是否更喜欢Excel功能或VBA解决方案。两者都有相当简单的答案。在这两种情况下,您都可以通过使用find函数来简化搜索字符的字符串索引。在Excel函数中,它被称为Find
,而在VBA中,非常接近的等价物是InStr
。您只需找到分隔符的两个匹配项并连接剩余字符串的左右边缘。
在Excel中,函数可能是“A1”是您的单元格地址:
=LEFT(A1,FIND("/",A1)-1) & RIGHT(A1,LEN(A1)-FIND("/",A1,FIND("/",A1)+1))
或者在VBA中,等价物是:
Public Function TextOutsideDelimeters(txt As String, sep As String) As String
Dim pt1 As Long
Dim pt2 As Long
pt1 = InStr(txt, sep)
pt2 = InStr(pt1 + 1, txt, sep)
TextOutsideDelimeters = Left(txt, pt1 - 1) & _
Right(txt, Len(txt) - pt2)
End Function
答案 1 :(得分:2)
这样的事情怎么样:
Option Explicit
Public Sub tmpSO()
Dim i As Long
Dim strTMP As String
Dim strFinal As String
Dim strArray() As String
strTMP = "This is a line. /delete words in the area /keep this part /yet another part to delete / keep this one too / delete me."
strArray = Split(strTMP, "/")
For i = LBound(strArray) To UBound(strArray)
If i Mod 2 = 0 And i <> UBound(strArray) Then
strFinal = strFinal & strArray(i)
End If
Next i
Debug.Print strFinal
End Sub
答案 2 :(得分:2)
[A1].Replace "/*/", "", xlPart