修改表列中的字符串

时间:2014-05-20 18:30:06

标签: excel vba excel-vba

我正在尝试编写一个修改

字符串的宏

“evice& Services帮助和故障排除:设备帮助和故障排除:设备解锁代码请求205 613-2860”

“设备和服务帮助和故障排除设备帮助和故障排除设备解锁代码请求”

这可以纠正第一个单词中的拼写错误,最后删除冒号和电话号码。

我尝试了工作表函数,并且以下函数可以工作:

=SUBSTITUTE([@Description], ":", " ")

=SUBSTITUTE([@Description], "evice", "Device")

=LEFT([@Description], LEN([@[R-val]])-13)

我遇到错误类型13“类型不匹配”的问题。

我也假设因为原始字符串在一个表中,我不必进行循环。

我的VBA代码是

Sub rmvstuff()
Dim needsrd As Worksheet
Dim device As String
Dim rmvcln As String
Dim rmvtpnum As String
Dim desc As Range
Dim mycell As String

mycell = ActiveCell.Value



With needsrd
    Set desc = Range("description")
End With

desc.Select
device = WorksheetFunction.Substitute([@Description], "evice", "Device")
rmvcln = WorksheetFunction.Substitute([@Description], ":", " ")
rmvtpnum = WorksheetFunction.Left([@Description], Len([@Description]) - 13)
End Sub

我真的很感激我能得到的任何帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

您有一些错误,例如needsrd从未声明,因此它等于Nothing。此外,@Description不是VBA中的有效标识符。

这是另一种做你尝试的方法:

Sub rmvstuff()
    Dim needsrd As Worksheet
    Dim desc As Range

    Set needsrd = Sheets(1)
    Set desc = needsrd.Range("description")

    desc.Value = Replace(desc.Value, "evice", "Device")
    desc.Value = Replace(cell.Value, "DD", "D")
    desc.Value = Replace(desc.Value, ":", "")
    desc.Value = Left(desc.Value, Len(desc.Value) - 13)
End Sub

以下是如何循环遍历每个单元格,假设“description”是多个单元格的命名范围:

Sub loopRMVstuff()
    Dim needsrd As Worksheet
    Dim desc As Range

    Set needsrd = Sheets(1)
    Set desc = needsrd.Range("description")

    For Each cell In desc
        cell.Value = Replace(cell.Value, "evice", "Device")
        cell.Value = Replace(cell.Value, "DD", "D")
        cell.Value = Replace(cell.Value, ":", "")
        cell.Value = Left(cell.Value, Len(cell.Value) - 13)
    Next cell
End Sub

使用单元格A1:A3

的命名范围

经过测试

之前 enter image description here

enter image description here