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
上面代码的问题是,当我使用调用测试(“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”令牌
答案 0 :(得分: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
答案 1 :(得分:0)
使用:
Formula1:= "=EXACT(A1;""" & sToken & """)"
或者:
Formula1:="=EXACT(" & Cells(1, 1).Address(False, False, xlA1) & ";""" & sToken & """)"
如果您想申请子范围,您只需更改该部分即可。
答案 2 :(得分:0)
嗯,经过对几个论坛的深入阅读,我找到了我需要的东西
这里的解决方案适用于许多不同的情况:设置自定义事件处理程序=)
从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:这里有一些帮助我很大的链接
How to create application-level event handlers in Excel
Controlling multiple textboxes on a userform