简体   繁体   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

The problem with the code above is, when I use Call test("a") (for example) I get formatted cells with "a" and "A", but I want just an "a". 上面代码的问题是,当我使用Call test(“ a”)时 (例如),我得到带有“ a”和“ A”的格式化单元格,但是我只想要一个“ a”。
Any suggestions? 有什么建议么?

PS: not skilled in VBA and English, please don't kill =) PS:不精通VBA和英语,请不要杀死=)


Ok, here the full macro for better understanding problem (with my crappy coding skills =P ) 好的,这里是完整的宏,可以更好地理解问题(我的编程技巧很差= 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

Macro do the job, but not with "t" and "T" tokens 宏可以完成这项工作,但不能使用“ t”和“ T”令牌

Explicitly specify Upper Case , Lower Case formatting. 明确指定Upper CaseLower Case格式。

Add the condition to check, 添加条件进行检查,

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

EDIT 编辑

This works: 这有效:

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

But this doesn't: 但这不是:

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

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

Or: 要么:

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

If you want to apply to a subrange you can simply change that part. 如果要应用到子范围,则只需更改该部分。

Well, after some deep readings of couple forums I found what I needed. 好吧,在深入阅读了几个论坛之后,我发现了我所需要的。
Here the solution, suitable in many different situations: set custom event handlers =) 这里的解决方案适用于许多不同的情况:设置自定义事件处理程序=)
Steps for set Worksheet events from VBA: 从VBA设置Worksheet事件的步骤:
1. Create class module, which will be Your event handler (named clsWorksheetEventHandler in my case) 2. Code him: 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. In Your working module add subroutines to initialize and terminate handling: 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. ????????????????????? 4. ?????????????????????
5. PROFIT!!!!!! 5.利润!!!!!!

I hope You enjoy =) 我希望你喜欢=)


PS: Here are some links that have helped me greatly PS:这是一些对我有很大帮助的链接
How to create application-level event handlers in Excel 如何在Excel中创建应用程序级事件处理程序
Controlling multiple textboxes on a userform 在用户窗体上控制多个文本框

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

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