當(dāng)工作表里面的數(shù)據(jù)量大的時候,鼠標(biāo)選定當(dāng)前單元格后,要想弄清楚它對應(yīng)的行和列實(shí)在是不方便。如果能做到excel自動以高亮突出顏色的方式來顯示當(dāng)前行和列的話,那該多方便啊,哈哈。其實(shí)excel中可以做到這個效果的,方法有兩種:條件格式和vba。
使用條件格式
下面將使用條件格式實(shí)現(xiàn)當(dāng)前單元格和當(dāng)前單元所在行或列突出顯示的效果。
在公式中使用了cell函數(shù),并將screenupdation屬性的值設(shè)置為true以實(shí)現(xiàn)屏幕更新。
在thisworkbook模塊中輸入如下代碼: private sub workbook_sheetselectionchange(byval sh as object, byval target as range) application.screenupdating = true end sub 當(dāng)然,如果只想在工作表中實(shí)現(xiàn)突出顯示的效果,將application.screenupdating = true代碼輸入到worksheet_selectionchange事件中。
接下來,在excel中選擇菜單“格式——條件格式”命令,彈出“條件格式”對話框。
在“條件”下拉框中選擇“公式”并在右側(cè)中輸入下面相應(yīng)的公式,然后點(diǎn)擊“格式”按鈕,設(shè)置相應(yīng)的格式,完成后,按“確定”按鈕。
下面是相應(yīng)的公式及其作用: (1)公式“=cell("address")=address(row(),column())”,突出活動單元格,如下圖1所示。
圖1
(2)公式“=cell("row")=row()”,突出單元格所在行,如下圖2所示。
圖2
(3)下面的公式突出到當(dāng)前單元格為止的相應(yīng)行和列,呈反l形,如下圖3所示。 “=or(and(cell("row")=row(),cell("col")+1>column()),and(cell("col")=column(),cell("row")+1>row()))”
圖3
(4)在“條件格式”對話框中對所選單元格區(qū)域設(shè)置兩個如下所列的公式條件,將呈反l形突出顯示到當(dāng)前單元格為止的相應(yīng)行和列,且當(dāng)前單元格背景色改變、字體加粗顯示,如下圖4所示。 “=cell("address")=address(row(),column())” “=or(and(cell("row")=row(),cell("col")+1>column()),and(cell("col")=column(),cell("row")+1>row()))”
使用vba代碼
(1) 突出顯示至當(dāng)前單元格所在的行和列,呈反l形。在需要設(shè)置此功能的工作表模塊中輸入下面的代碼:
private sub worksheet_selectionchange(byval target as range)
dim icolor as integer
'注:如果工作表中有想要保留的條件格式,則不要使用本程序
'忽略用戶選擇單元格區(qū)域時可能產(chǎn)生的錯誤
on error resume next
icolor = target.interior.colorindex
if icolor < 0 then
icolor = 36
else
icolor = icolor + 1
end if
'避免字體顏色與突出色相同
if icolor = target.font.colorindex then icolor = icolor + 1
cells.formatconditions.delete
'水平突出色
with range("a" & target.row, target.address)
.formatconditions.add type:=2, formula1:="true"
.formatconditions(1).interior.colorindex = icolor
end with
'垂直突出色
with range(target.offset(1 - target.row, 0).address & ":" & _
target.offset(-1, 0).address)
.formatconditions.add type:=2, formula1:="true"
.formatconditions(1).interior.colorindex = icolor
end with
end sub
注意,此代碼運(yùn)行后,將清除所在工作表中含有的條件格式。示例文檔見 用顏色自動突出顯示當(dāng)前單元格行列1.xls。uploadfiles/2006-10/1027319888.rar
(2) 突出顯示當(dāng)前單元格所在的行和列。在需要設(shè)置此功能的工作表模塊中輸入下面的代碼:
private sub worksheet_selectionchange(byval target as range)
'可帶條件格式,但不能復(fù)制/剪切/粘貼操作
with target.parent
.cells.interior.colorindex = 0
.columns(target.column).cells.interior.colorindex = 35
.rows(target.row).cells.interior.colorindex = 35
end with
end sub
注意,此代碼運(yùn)行后,在當(dāng)前工作表中不能進(jìn)行復(fù)制、剪切和粘貼功能。示例文檔見 用顏色自動突出顯示當(dāng)前單元格行列2.xls。uploadfiles/2006-10/1027121529.rar
(3) 突出顯示當(dāng)前單元格所在的行和列。在需要設(shè)置此功能的工作表模塊中輸入下面的代碼:
private sub worksheet_change(byval target as range)
if application.cutcopymode <> false then
application.cutcopymode = false
call colorband(target)
end if
end sub
‘- - - - - - - - - - - - - - - - - - - - -
private sub worksheet_selectionchange(byval target as range)
if application.cutcopymode = false then
call colorband(target)
else
exit sub
end if
end sub
‘- - - - - - - - - - - - - - - - - - - - -