删除VBA中的一部分Cell值

时间:2014-09-15 09:36:42

标签: vba excel-vba excel

我正在尝试从单元格值中删除一部分,但它不起作用。

单元格的示例内容:(样本输入)

="*sls*lmt"&CONCATENATE("_",'C:\Users\Administrator\AppData\Roaming\Microsoft\AddIns\XL-EZ Addin.xla'!COLUMNLETTER(C14),TEXT(ROW(),"0"))&"_sel*range(status)*0*GOOD|OK|0|#00D700|0|100|80*NOT GOOD|NG|0|#FFFF00|0|79|50*FAULTED|FAULTED|0|#FF0000|1|49|0"

示例输出:

="*sls*lmt"&CONCATENATE("_",!COLUMNLETTER(C14),TEXT(ROW(),"0"))&"_sel*range(status)*0*GOOD|OK|0|#00D700|0|100|80*NOT GOOD|NG|0|#FFFF00|0|79|50*FAULTED|FAULTED|0|#FF0000|1|49|0"

我希望从值

中删除此部分
'C:\Users\Administrator\AppData\Roaming\Microsoft\AddIns\XL-EZ Addin.xla'

这是我试过的

Sub UpdateSheetButton()
Dim subStr1 As String
Dim subSrrt2 As String

Dim pos As Integer

For Each Cell In ActiveSheet.UsedRange.Cells
pos = InStr(Cell.Value, "'C:\")
If pos >= 1 Then
  subStr1 = Split(Cell.Value, "'C:\", 1)
  subStr2 = Split(Cell.Value, "\AddIns\XL-EZ Addin.xla'", 2)
  Cell.Value=subStr1 +subStr2 
  End If
Next

End Sub

错误: 运行时错误' 13' 类型不匹配 在这一行

pos = InStr(Cell.Value, "'C:\")

3 个答案:

答案 0 :(得分:6)

我想知道你为什么要用VBA来完成这么简单的任务。您可以使用内置方法。

WAY One(非VBA)

CTRL + H 以显示Find and Replace对话框,并使用该对话框替换文本。

第二种方式(VBA)

如果您仍想使用VBA,则只需录制上述宏。有不需要来使用循环。例如

Sub Sample()
    Dim sFind As String
    Dim sNew As String

    '~~> Example. Chnage as applicable
    sFind = "Sum"
    sNew = "Max" 'or sNew = "" in case you want to remove the above text

    '~~> For Complete Worksheet
    With ThisWorkbook.Sheets("Sheet1").Cells
        .Replace What:=sFind, Replacement:=sNew, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    End With

    '~~> For a Column
    With ThisWorkbook.Sheets("Sheet1").Columns(1)
        .Replace What:=sFind, Replacement:=sNew, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    End With

    '~~> For a range
    With ThisWorkbook.Sheets("Sheet1").Range("A1:A10")
        .Replace What:=sFind, Replacement:=sNew, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    End With
End Sub

答案 1 :(得分:2)

类型不匹配错误的原因是:Split返回一个数组,但您将其分配给字符串。所以你需要声明你的变量:

Dim subStr1() As String
Dim subStr2() As String ' caution: there is a typo here in your code

此外,Split使用起来有点笨拙。我会选择InStrMid之类的字符串函数。

此外,如果我理解正确,您正在尝试修改单元格的公式。但是,您现有的代码会查看单元格的。不一样的事情。将.Value更改为.Formula

最后,您的示例输出不是有效的公式。摆脱!之前的COLUMNLETTER

="*sls*lmt"&CONCATENATE("_",!COLUMNLETTER(C14),TEXT(ROW(),"0"))&"_sel*range(status)*0*GOOD|OK|0|#00D700|0|100|80*NOT GOOD|NG|0|#FFFF00|0|79|50*FAULTED|FAULTED|0|#FF0000|1|49|0"
                            ^
                          no good

考虑到所有这些,完成任务的更好方法是:

Function RemoveTextBetween(ByVal strInput As String, _
    ByVal strStartCut As String, ByVal strEndCut As String) As String

    Dim iStartCut As Long
    Dim iEndCut As Long
    iStartCut = InStr(strInput, strStartCut)
    iEndCut = InStr(strInput, strEndCut) + Len(strEndCut)
    RemoveTextBetween = Left(strInput, iStartCut - 1) & Mid(strInput, iEndCut)
End Function

使用示例:

cell.Formula = RemoveTextBetween(cell.Formula, "'C:\", "\AddIns\XL-EZ Addin.xla'!")

请注意我最后添加了!以确保它被删除。

答案 2 :(得分:0)

您可以使用Replace替换单元格Formula属性中的文本,即

A1    B1    C1    D1    E1
 a     b     c     d     =CONCATENATE(A1,B1,C1,D1,"Something Else")

假设我想从公式中删除“Something Else”:

Sub removefromformula()

    Dim r As Range
    Set r = Me.[E1]

    Debug.Print r.Formula

    r.Formula = Replace(r.Formula, ",""Something Else""", "")

    Debug.Print r.Formula

End Sub

返回:

=CONCATENATE(A1,B1,C1,D1,"Something Else")
=CONCATENATE(A1,B1,C1,D1)

在即时窗口中。