繁体   English   中英

需要区分大小写的格式(Excel)

[英]Need case-sensitive formatting (Excel)

Sub test(sToken As String)
    Cells.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:=sToken
    Cells.FormatConditions(Cells.FormatConditions.Count).SetFirstPriority
    With Cells.FormatConditions(1).Interior
        .Pattern = xlPatternLightVertical
        .PatternColorIndex = 4
        .ColorIndex = 10
    End With
    Cells.FormatConditions(1).StopIfTrue = False
End Sub

上面代码的问题是,当我使用Call test(“ a”)时 (例如),我得到带有“ a”和“ A”的格式化单元格,但是我只想要一个“ a”。
有什么建议么?

PS:不精通VBA和英语,请不要杀死=)


好的,这里是完整的宏,可以更好地理解问题(我的编程技巧很差= P)

Sub FormatTokens()
    Call FormatReset   'Clear formatting
    Call SetFormatting("d", xlPatternNone, 1, 44)
    Call SetFormatting("h", xlPatternCrissCross, 46, 44)
    Call SetFormatting("t", xlPatternLightVertical, 4, 10) ' Here the 1st conflict token 
    Call SetFormatting("p", xlPatternNone, 1, 10)
    Call SetFormatting("T", xlPatternNone, 4, 10) ' And here another
    Call SetFormatting("v", xlPatternGray16, 49, 24)
' Blah, blah, blah in the same style...
End Sub
Private Sub SetFormatting(sToken As String, oPat As XlPattern, iPatCol As Integer, iCol As Integer)
    Cells.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:=sToken
    Cells.FormatConditions(Cells.FormatConditions.Count).SetFirstPriority
    With Cells.FormatConditions(1).Interior
        .Pattern = oPat
        .PatternColorIndex = iPatCol
        .ColorIndex = iCol
    End With
    Cells.FormatConditions(1).StopIfTrue = False
End Sub

宏可以完成这项工作,但不能使用“ t”和“ T”令牌

明确指定Upper CaseLower Case格式。

添加条件进行检查,

if UCase(range.value) = UCase(sToken) then 
// do formatting
end if

编辑

这有效:

Cells.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=EXACT($B1,""a"")"

但这不是:

sToken = "=EXACT($A1, """"" & sToken & """"")"
Cells.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:=sToken

用法: Formula1:= "=EXACT(A1;""" & sToken & """)"

要么:

Formula1:="=EXACT(" & Cells(1, 1).Address(False, False, xlA1) & ";""" & sToken & """)"

如果要应用到子范围,则只需更改该部分。

好吧,在深入阅读了几个论坛之后,我发现了我所需要的。
这里的解决方案适用于许多不同的情况:设置自定义事件处理程序=)
从VBA设置Worksheet事件的步骤:
1.创建类模块,将其作为您的事件处理程序(在我的案例中为clsWorksheetEventHandler )2.为他编码:

Option Explicit

Public WithEvents WorksheetEvents As Worksheet 'As an object whose events should be handled

Private Sub WorksheetEvents_Change(ByVal Target As Range) 'Event to handle
    'Some code You need to handle this event
End Sub

3.在工作模块中,添加子例程以初始化和终止处理:

Option Explicit

Dim oWorksheetEventHandler As clsWorksheetEventHandler 'Ref for Your class
Dim colWorksheetEventHandlers As Collection 'For all referrals

Sub WorksheetEventHandlers_initialize()
    'Create new Collection to store ours handlers
    Set colWorksheetEventHandlers = New Collection 
    'Loop through worksheets
    For Each Worksheet In Worksheets
        'Create new instance of the event handler class
        Set WorksheetEventHandler = New clsWorksheetEventHandler 
        'Set it to handle events in worksheet
        Set WorksheetEventHandler.WorksheetEvents = Worksheet
        'And add it to our collection
        colWorksheetEventHandlers.Add WorksheetEventHandler
    Next Worksheet
End Sub

Sub WorksheetEwentHandlers_terminate()
    'Loop through our collection
    For Each WorksheetEventHandler In colWorksheetEventHandlers
    'Clear event handler
    Set WorksheetEventHandler = Nothing
Next WorksheetEventHandler
'And finally clear memory
Set colWorksheetEventHandlers = Nothing
End Sub

4. ?????????????????????
5.利润!!!!!!

我希望你喜欢=)


PS:这是一些对我有很大帮助的链接
如何在Excel中创建应用程序级事件处理程序
在用户窗体上控制多个文本框

暂无
暂无

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

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