0
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”标记

4

3 回答 3

1

明确指定Upper Case,Lower 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
于 2012-12-24T20:10:09.173 回答
0

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

或者:

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

如果你想应用到一个子范围,你可以简单地改变那个部分。

于 2012-12-24T23:06:57.227 回答
0

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

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 中创建应用程序级事件处理
程序控制用户窗体上的多个文本框

于 2012-12-28T00:17:11.857 回答