[英]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 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
用法: 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.