[英]How do I use the Characters Object in Excel VBA with a variable rather than a cell reference
I am trying to return each character in a string.我正在尝试返回字符串中的每个字符。 I know that the same can be accomplished by making use of MID but, this is just a small part of a bigger script for which I need to determine the characters in italic.我知道同样可以通过使用 MID 来完成,但这只是我需要确定斜体字符的较大脚本的一小部分。
The following works correct:以下工作正确:
For x = 1 To Len(Activecell)
MsgBox Activecell.Characters(x, 1).Text
Next x
However trying to do the same with a variable in which the string is stored does not work:但是,尝试对存储字符串的变量执行相同操作是行不通的:
V = "blabla blabla blabla"
For x = 1 To Len(V)
MsgBox V.Characters(x, 1).Text
Next x
Is there a way to use the Characters object in combination with a variable in which the string is stored?有没有办法将字符 object 与存储字符串的变量结合使用?
So it seems you don't want to change your values in a cell, but rather in a variable.因此,您似乎不想更改单元格中的值,而是更改变量中的值。 However, font information is stored in a cell containing a value.但是,字体信息存储在包含值的单元格中。 And since Characters
is a Range
property, it won't work on a variable.由于Characters
是Range
属性,因此它不适用于变量。 You'd need to iterate the characters in the actual cell and change your variable accordingly.您需要迭代实际单元格中的字符并相应地更改您的变量。 For example, let's say this is cell A1
:例如,假设这是单元格A1
:
Sub Test()
Dim V As String: V = [A1]
For x = 1 To Len(V)
If Range("A1").Characters(x, 1).Font.Italic Then
V = Application.Replace(V, x, 1, "k")
End If
Next x
Debug.Print V
End Sub
Returns:回报:
blakla blakla klabla
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.