简体   繁体   English

使用VBA格式化单元格中的特定文本?

[英]Format specific text within a cell with VBA?

When I use Excel it allows me to set specific words within a cell to bold or italic or even change the text size. 当我使用Excel时,它允许我将单元格中的特定单词设置为粗体斜体 ,甚至更改文本大小。

Is there a way of doing this in VBA? 在VBA中有没有办法做到这一点?

I have two cells with text. 我有两个带有文字的单元格。 One cell contains a list of words which I separated into an array. 一个单元格包含一个单词列表,我将它们分成一个数组。 The other cell contains a few sentences. 另一个单元格包含一些句子。

I want to write a macro that highlights all words from cell 1 in cell 2. 我想编写一个突出显示单元格2中单元格1中所有单词的宏。

My idea was to use the array and InStr to search for the position of my words in cell 2. Once found I wanted to split cell 2, format one word an put everything back together. 我的想法是使用数组和InStr搜索单词在单元格2中的位置。找到后,我想拆分单元格2,格式化一个单词,然后将所有内容放回去。

Maybe this is possible via Word? 也许可以通过Word进行?

I believe the answer could be found here: excel vba: make part of string bold 我相信答案可以在这里找到: excel vba:将字符串的一部分设为粗体

Specifically, 特别,

ActiveCell.FormulaR1C1 = "name/A/date" & Chr(10) & "name/B/date" & Chr(10) & "name/C/date"
With ActiveCell.Characters(Start:=25, Length:=4).Font
    .FontStyle = "Bold"
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM