Excel VBA:如何从单元格中删除子字符串? [英] Excel VBA: How to remove substrings from a cell?

查看:582
本文介绍了Excel VBA:如何从单元格中删除子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的单元格值:

I have a cell value like this:

This is a <"string">string, It should be <"changed">changed to <"a"> a number.

在此部分<" ">中重复了一些单词.

There are some words repeated in this part <" ">.

我想使用Excel VBA将单元格值更改为:

I want use Excel VBA to change the cell value to:

This is a string, It should be changed to a number.

任何帮助将不胜感激.谢谢.

Any help will be appreciated. Thanks.

推荐答案

假定单元格A1中的文本,然后尝试使用此代码

Assuming the text in cell A1, then try this code

Sub DelDoubleString()
Dim Text As String, Text2Replace As String, NewText As String
On Error Resume Next        'Optional, in case there's no double string to be deleted
Text = Cells(1, 1)

Do
    Text2Replace = Mid$(Text, InStr(Text, "<"), InStr(Text, ">") - InStr(Text, "<") + 1)
    NewText = Application.WorksheetFunction.Substitute(Text, Text2Replace, vbNullString)
    Text = NewText
Loop Until InStr(NewText, "<") = 0

Cells(1, 1) = NewText

End Sub

这篇关于Excel VBA:如何从单元格中删除子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆