2016-08-21 89 views
0

我有一个单元格的值是这样的:Excel VBA:如何从单元格中删除子字符串(具有相同的开始和结束符号)?

This is a line. /delete words in the area /keep this part 

可以说细胞是A1,单元格值包含两个/秒。我想使用Excel VBA它们之间删除字母和单元格的值更改为:

This is a line. keep this part 

其实我问过类似的问题之前:Excel VBA: How to remove substrings from a cell?

有我有两个不同的符号来定位子,但在这里我有两个相同的符号。然后我不知道如何重写代码。

任何帮助将得到通过。谢谢!

回答

3

你不提你是否宁愿一个Excel函数或VBA解决方案。两者都有相当直接的答案。在这两种情况下,您都可以通过使用查找函数来查找简单的搜索字符的字符串索引。在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 
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
[A1].Replace "/*/", "", xlPart 
相关问题