删除特殊字符VBA Excel [英] Removing special characters VBA Excel

查看:694
本文介绍了删除特殊字符VBA Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VBA阅读一些TITLES,然后将该信息复制到powerpoint演示文稿。

I'm using VBA to read some TITLES and then copy that information to a powerpoint presentation.

我的问题是,TITLES有特殊字符,但Image我也正在处理的文件不要。

My Problem is, that the TITLES have special characters, but Image files that I am also coping over do not.

TITLE是将JPEG加载到图片容器的路径的一部分。例如。 P k.jpg,但标题称为pk。

The TITLE forms part of a path to load a JPEG into a picture container. E.g. "P k.jpg", but the title is called "p.k".

我想要忽略TITLE中的特殊字符,只需让它看到一个空格,所以它拿起正确的JPG文件。

I want to be able to ignore the special characters in the TITLE and just get it to see a space instead so it picks up the right JPG file.

可以吗?

谢谢! / p>

Thank you!

推荐答案

你认为特殊的字符是什么,只是简单的标点符号?您应该可以使用替换函数:替换(p.k,。,)

What do you consider "special" characters, just simple punctuation? You should be able to use the Replace function: Replace("p.k","."," ").

Sub Test()
Dim myString as String
Dim newString as String

myString = "p.k"

newString = replace(myString, ".", " ")

MsgBox newString

End Sub

如果你有几个字符,你可以在自定义函数或简单链接的替换函数等。

If you have several characters, you can do this in a custom function or a simple chained series of Replace functions, etc.

  Sub Test()
Dim myString as String
Dim newString as String

myString = "!p.k"

newString = Replace(Replace(myString, ".", " "), "!", " ")

'## OR, if it is easier for you to interpret, you can do two sequential statements:
'newString = replace(myString, ".", " ")
'newString = replace(newString, "!", " ")

MsgBox newString

End Sub

如果您有很多潜在的特殊字符(例如非英语重音ascii),您可以执行自定义函数或迭代ove一个数组。

If you have a lot of potential special characters (non-English accented ascii for example?) you can do a custom function or iteration over an array.

Const SpecialCharacters As String = "!,@,#,$,%,^,&,*,(,),{,[,],},?"  'modify as needed
Sub test()
Dim myString as String
Dim newString as String
Dim char as Variant
myString = "!p#*@)k{kdfhouef3829J"
newString = myString
For each char in Split(SpecialCharacters, ",")
    newString = Replace(newString, char, " ")
Next
End Sub

这篇关于删除特殊字符VBA Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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