电竞比分网-中国电竞赛事及体育赛事平台

分享

Excel讓選定單元格所在行和列顏色突出高亮顯示

 昵稱7665211 2014-01-12

Excel讓選定單元格所在行和列顏色突出高亮顯示


當工作表里面的數(shù)據(jù)量大的時候,鼠標選定當前單元格后,要想弄清楚它對應的行和列實在是不方便。如果能做到excel自動以高亮突出顏色的方式來顯示當前行和列的話,那該多方便啊,哈哈。其實excel中可以做到這個效果的,方法有兩種:條件格式和vba。

使用條件格式

下面將使用條件格式實現(xiàn)當前單元格和當前單元所在行或列突出顯示的效果。

在公式中使用了cell函數(shù),并將screenupdation屬性的值設置為true以實現(xiàn)屏幕更新。

在thisworkbook模塊中輸入如下代碼: private sub workbook_sheetselectionchange(byval sh as object, byval target as range) application.screenupdating = true end sub 當然,如果只想在工作表中實現(xiàn)突出顯示的效果,將application.screenupdating = true代碼輸入到worksheet_selectionchange事件中。

接下來,在excel中選擇菜單“格式——條件格式”命令,彈出“條件格式”對話框。

在“條件”下拉框中選擇“公式”并在右側(cè)中輸入下面相應的公式,然后點擊“格式”按鈕,設置相應的格式,完成后,按“確定”按鈕。

下面是相應的公式及其作用: (1)公式“=cell("address")=address(row(),column())”,突出活動單元格,如下圖1所示。

 

圖1

(2)公式“=cell("row")=row()”,突出單元格所在行,如下圖2所示。

 

圖2

(3)下面的公式突出到當前單元格為止的相應行和列,呈反l形,如下圖3所示。 “=or(and(cell("row")=row(),cell("col")+1>column()),and(cell("col")=column(),cell("row")+1>row()))”

 

圖3

(4)在“條件格式”對話框中對所選單元格區(qū)域設置兩個如下所列的公式條件,將呈反l形突出顯示到當前單元格為止的相應行和列,且當前單元格背景色改變、字體加粗顯示,如下圖4所示。 “=cell("address")=address(row(),column())” “=or(and(cell("row")=row(),cell("col")+1>column()),and(cell("col")=column(),cell("row")+1>row()))”

  圖4  示例文檔見 使用條件格式自動突出顯示.xls。

使用vba代碼
(1) 突出顯示至當前單元格所在的行和列,呈反l形。在需要設置此功能的工作表模塊中輸入下面的代碼:
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
注意,此代碼運行后,將清除所在工作表中含有的條件格式。示例文檔見 用顏色自動突出顯示當前單元格行列1.xls。uploadfiles/2006-10/1027319888.rar
(2) 突出顯示當前單元格所在的行和列。在需要設置此功能的工作表模塊中輸入下面的代碼:
private sub worksheet_selectionchange(byval target as range)
  '可帶條件格式,但不能復制/剪切/粘貼操作
  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
注意,此代碼運行后,在當前工作表中不能進行復制、剪切和粘貼功能。示例文檔見 用顏色自動突出顯示當前單元格行列2.xls。uploadfiles/2006-10/1027121529.rar
(3) 突出顯示當前單元格所在的行和列。在需要設置此功能的工作表模塊中輸入下面的代碼:
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
‘- - - - - - - - - - - - - - - - - - - - -

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多