|
先看一下視頻效果: 下面詳細講解實現(xiàn)方法。 一、常見的聚光燈效果實現(xiàn)方法:“條件格式” VBA代碼法1、條件格式設(shè)置:開始——條件格式——新建規(guī)則——使用公式確定單元格格式,輸入公式:=OR(CELL('row')=ROW(),CELL('col')=COLUMN()),應(yīng)用范圍:=$1:$1048576。 注意復制粘貼公式后最好檢查一下公式的最終效果,有時候粘貼公式時,某一步?jīng)]操作好,系統(tǒng)會自動對公式添加一些雙引號什么的,比如把公式變?yōu)?'OR(CELL(''row'')=ROW(),CELL(''col'')=COLUMN())'等,會導致效果出不來,遇到這種情況時,不用慌,比照上面的公式,將多余的符號直接刪掉即可。 2、VBA代碼編輯:打開VBE——雙擊對應(yīng)表單——粘貼代碼: Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Application.Calculate
Application.ScreenUpdating = True
End Sub
中間的兩行代碼任用一行即可。案例中行和列為相同的顏色,我們還可以設(shè)置為不同的顏色。 二、擴展1:設(shè)置聚光燈行、列及單元格為三種不同顏色。設(shè)置三次條件格式,方法同上,分別設(shè)置行、列、單元格的顏色,對應(yīng)公式分別為: 行對應(yīng)公式:=CELL('row')=ROW(); 列對應(yīng)公式:=CELL('col')=COLUMN(); 單元格對應(yīng)公式:=AND(CELL('row')=ROW(),CELL('col')=COLUMN())。 代碼不用修改,設(shè)置過程及效果為: 三、純代碼方式聚光燈:不必設(shè)置條件格式,直接用代碼實現(xiàn)單元格所在行列變顏色,還能實現(xiàn)多個單元格所在行列一起變色顯示第一種方法當一次性選中了多個單元格時,僅能顯示第一個單元格對應(yīng)行列變色,通過直接在代碼里設(shè)置行列變色效果的方式,可一步實現(xiàn)聚光燈效果,而不必設(shè)置條件格式。代碼如下: Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Application.CutCopyMode = False Then With Target .Parent.Cells.Interior.ColorIndex = xlNone .EntireRow.Interior.Color = vbGreen .EntireColumn.Interior.Color = vbCyan .Interior.Color = vbRed End With End If End Sub 條件語句設(shè)置為當進行復制粘貼時,聚光燈暫不啟用。 效果為: 總的來說,相比第一種方法,純代碼法更簡單,只是當想修改行列的顯示顏色時,需要在代碼中修改,不太方便,好在一般我們不會經(jīng)常換顏色。 四、擴展2:聚光燈功能開啟和關(guān)閉隨時切換有時候,我們希望中途不想用聚光燈模式,我們可通過設(shè)置一個按鈕來控制聚光燈功能的隨時關(guān)閉和開啟。效果為: 實現(xiàn)過程: 1、開發(fā)工具——插入——ActiveX控件復選框——在設(shè)計模式下右鍵復選框——查看代碼,打開代碼輸入界面,粘貼下面的代碼: Private Sub CheckBox1_Click()
If CheckBox1.Value = False Then
CheckBox1.Caption = '關(guān)'
ActiveSheet.Cells.Interior.ColorIndex = xlNone
Else
CheckBox1.Caption = '開'
End If
End Sub
2、在表單Worksheet_SelectionChange代碼編輯區(qū),修改代碼: Private Sub Worksheet_SelectionChange(ByVal target As Range) If CheckBox1.Caption = '開' Then Call 聚光燈(target) End Sub Sub 聚光燈(rg As Range) If Application.CutCopyMode = False Then With rg .Parent.Cells.Interior.ColorIndex = xlNone .EntireRow.Interior.Color = vbGreen .EntireColumn.Interior.Color = vbCyan .Interior.Color = vbRed End With End If End Sub 五、擴展3:追光燈效果:實現(xiàn)代碼分三部分: 1、開關(guān)設(shè)置代碼: Private Sub CheckBox1_Click() If CheckBox1.Value = False Then CheckBox1.Caption = '關(guān)' On Error Resume Next ActiveSheet.Cells.Interior.ColorIndex = xlNone ActiveSheet.Shapes.Range(Array('箭頭000')).Delete Else CheckBox1.Caption = '開' End If End Sub 2、調(diào)用代碼: Private Sub Worksheet_SelectionChange(ByVal target As Range) If CheckBox1.Caption = '開' Then Call 追光燈(target) End If End Sub 3、功能代碼: Sub 追光燈(rg As Range) On Error Resume Next rg.Parent.Cells.Interior.ColorIndex = xlNone ActiveSheet.Shapes.Range(Array('箭頭000')).Delete ActiveSheet.Shapes.AddConnector(msoConnectorStraight, 0, 0, _ rg.Left, rg.Top).Select Selection.ShapeRange.Name = '箭頭000' With Selection.ShapeRange.Line .Visible = msoTrue .Weight = 10.25 End With With Selection.ShapeRange.Line .Visible = msoTrue .ForeColor.ObjectThemeColor = msoThemeColorAccent1 .ForeColor.TintAndShade = 0 .ForeColor.Brightness = 0 .Transparency = 0.6 End With rg.Interior.Color = vbRed End Sub |
|
|