简体   繁体   English

如何将 Excel VBA 中的字符 Object 与变量而不是单元格引用一起使用

[英]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.由于CharactersRange属性,因此它不适用于变量。 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.

相关问题 如何让我的 VBA 项目参考 OneDrive 上的 Excel 工作簿以使用本地驱动器路径而不是 OneDrve URL 路径? - How do I get my VBA project reference to an Excel Workbook on OneDrive to use the local drive path rather than the OneDrve URL Path? 如何使用 VBA 通过列标题而不是工作表中单元格的位置来引用调用? - How do use VBA to reference a call by the heading of the column rather than the location of the cell in the sheet? Excel VBA - 如何引用单元格的可见内容而不是其公式? - Excel VBA - How do I refer to a cell's visible content rather than its formula? 如何使用vlookup搜索单元格值而不是其单元格引用公式(Excel) - How to use vlookup to search for a cell value rather than its cell reference formula(Excel) 使用Excel中的VBA,如何引用具有变量的单元格 - With VBA in Excel how can I reference a cell with a variable 如何在Excel的单元格中处理超过255个字符? - How do I handle more than 255 characters in a cell in Excel? 如何在Excel表单元格中更改值而不在VBA中指定单元格引用 - How do I change the value in an Excel table cell without specifying a cell reference in VBA Excel导出; 使用VBA,如何将单元格导出为格式而不是原始单元格值 - Excel export ; using VBA, how to export cell as formatted rather than the raw cell value 对于Excel VBA中的每个循环,我如何引用公式中的单元格地址? - With For each loop in Excel VBA how do I reference the cell address within the formula? 如何在VBA的共享点上引用存储在Excel文件中的单元格值 - How do I reference a Cell value stored in an excel file on sharepoint in VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM