VBA代码在excel中使标签之间的文本变粗 [英] VBA code in excel to made text between tags bold

查看:288
本文介绍了VBA代码在excel中使标签之间的文本变粗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个csv文件,其中包含html标签< b>和< \ b>表示粗体文本。 (即这些标签之间的几个字,在单元格中较长的文本块中,应该是粗体)。有没有办法使用excel中的vba代码来剥离标签,并使标签之间的文字变粗?



注意 - 给定单元格内有多个标签集合。

解决方案

这应该做你想要的:

  Sub BoldTags()
Dim X As Long,BoldOn As Boolean
BoldOn = False'从单元格开始的默认值不为粗体
对于X = 1 To Len ActiveCell.Text)
如果UCase(Mid(ActiveCell.Text,X,3))=&B然后
BoldOn = True
ActiveCell.Characters(X,3).Delete
End If
如果UCase(Mid(ActiveCell.Text,X,4))=< / B>中然后
BoldOn = False
ActiveCell.Characters(X,4).Delete
End If
ActiveCell.Characters(X,1).Font.Bold = BoldOn
Next
End Sub

目前设置为在活动单元上运行,您可以直接在一个循环来做一整列。您可以轻松地将此代码适用于其他HTML标签,用于单元格格式(即斜体等)



这是在我测试的单元格(减去<)后面的空格:样品& B>碲< / b> st< B>粗体< / B>结束



结果是:粗体结束 Te st

希望有助于


I have a csv file which includes the html tags < b > and <\ b > to signify bold text. (I.e several words between these tags, in a longer block of text within the cell, should be bold). Is there a way using vba code in excel to strip the tags, and make the text between the tags bold?

Note - There are sometime multiple sets of tags within a given cell.

解决方案

This should do what you want:

Sub BoldTags()
Dim X As Long, BoldOn As Boolean
BoldOn = False 'Default from start of cell is not to bold
For X = 1 To Len(ActiveCell.Text)
    If UCase(Mid(ActiveCell.Text, X, 3)) = "<B>" Then
        BoldOn = True
        ActiveCell.Characters(X, 3).Delete
    End If
    If UCase(Mid(ActiveCell.Text, X, 4)) = "</B>" Then
        BoldOn = False
        ActiveCell.Characters(X, 4).Delete
    End If
    ActiveCell.Characters(X, 1).Font.Bold = BoldOn
Next
End Sub

Currently set to run on the activecell, you can just plop it in a loop to do a whole column. You can easily adapt this code for other HTML tags for Cell formatting (ie italic etc)

This was in the cell I tested on (minus the space after <): Sample < b>Te< /b>st of < B>bolding< /B> end

The result was: Sample Test of bolding end

Hope that helps

这篇关于VBA代码在excel中使标签之间的文本变粗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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