更优雅的方式来替换文字并进行格式化 [英] More elegant way of replacing text and formatting it

查看:123
本文介绍了更优雅的方式来替换文字并进行格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有这个:

  [M3] .select'Range(M3)。像使用[]符号
totalrows = [H2] .CurrentRegion.Rows.Count
范围(m3:p& totalrows)。选择
[m2]。激活
'Green
With Application.ReplaceFormat.Interior
.PatternColorIndex = xlAutomatic
.Color = 5287936
End with
Selection.Replace What:=Green,Replacement :=Red,LookAt:= xlPart,_
SearchOrder:= xlByRows,MatchCase:= False,SearchFormat:= False,_
ReplaceFormat:= True

有没有更优雅的方式来实现?



我今天发现了喜欢它的简单性,并将其并入处理文本替换的其他部分。

  [M:P]。更改绿色,红色,xlPart 

但是,有一种简单的方法可以使用简单的语句将单元格背景颜色设置为绿色?如果单元格包含文本绿色,将单元格背景更改为绿色,并且不更改文本。
感谢您的建议!

解决方案

如果单元格包含文本绿色将单元格背景更改为绿色,不要更改文本。



我会使用这个我认为优雅的: p>

  [A1] .FormatConditions.Add xlExpression,,= A1 =绿色
使用[A1] .FormatConditions(1)
.Interior.Color = RGB(0,255,0)
.ModifyAppliesToRange [M:P]'~~>当然,更改这部分以适合
结束

不是一个班轮。 >

Right now I have this:

[M3].select 'Range("M3").Select originally, I like using the [ ] notation
totalrows = [H2].CurrentRegion.Rows.Count
Range("m3:p" & totalrows).Select
[m2].Activate
'Green
    With Application.ReplaceFormat.Interior
        .PatternColorIndex = xlAutomatic
        .Color = 5287936
    End With
    Selection.Replace What:="Green", Replacement:="Red", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=True

Is there a more elegant way of accomplishing this?

I found this today and I love it's simplicity and have incorporated it in other sections that deal with text replacements.

[M:P].Replace "Green", "Red", xlPart

But, is there an easy way of setting the cell background color to green with a simple statement like that? If the cell contains the text "Green", change the cell background to Green and don't change the text. Thanks for your suggestions!

解决方案

If the cell contains the text "Green", change the cell background to Green and don't change the text.

I'd use this which I think is elegant enough:

[A1].FormatConditions.Add xlExpression, , "=A1=""Green"""
With [A1].FormatConditions(1)
    .Interior.Color = RGB(0, 255, 0)
    .ModifyAppliesToRange [M:P] '~~> of course change this part to suit
End With

Not one liner though.

这篇关于更优雅的方式来替换文字并进行格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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