2013-03-27 30 views
1

我有一個工作簿設置到工作表Sheet是可編輯的,由於在多個列在列A和B列出某些名稱(至T的列C)日期;行1 & 2是一個標題,所以數據輸入從第3行開始。Excel中:隱藏如果在該行中沒有細胞被着色的行

使用INDIRECT公式作爲帶有條件格式的受保護頁的Sheet2是相同的,如果到期日即將到來,則強調某些單元格爲紅色或黃色。

我沒有經驗與VBA並一直在尋找符合以下條件的宏:

在Sheet2上而已,如果該行不包含有紅色或黃色的任何細胞,然後隱藏那些不着色行。

任何幫助將不勝感激。我只找到隱藏基於單列條件的行的代碼。

+0

嘗試檢查這..這是很難的question..http的://www.mrexcel.com/forum/excel-questions/84136-need-visual-basic-applications-hide-rows-have-yellow-fill。 html – Kasnady 2013-03-27 08:33:03

回答

4

這裏有一個小腳本讓你開始。它將遍歷每行的每一列並檢查每個單元格的顏色。如果找到任何顏色,該行將被跳過。如果沒有發現有任何顏色的單元格,該行將被隱藏。換句話說,所有全白行將被隱藏

CODE:

Public Sub HideUncoloredRows() 
    Dim startColumn As Integer 
    Dim startRow As Integer 

    Dim totalRows As Integer 
    Dim totalColumns As Integer 

    Dim currentColumn As Integer 
    Dim currentRow As Integer 

    Dim shouldHideRow As Integer 

    startColumn = 1  'column A 
    startRow = 1  'row 1 
    totalRows = Sheet2.Cells(Rows.Count, startColumn).End(xlUp).Row 

    For currentRow = totalRows To startRow Step -1 
     shouldHideRow = True 
     totalColumns = Sheet2.Cells(currentRow, Columns.Count).End(xlToLeft).Column 
     'for each column in the current row, check the cell color 
     For currentColumn = startColumn To totalColumns 
      'if any colored cell is found, don't hide the row and move on to next row 
      If Not Sheet2.Cells(currentRow, currentColumn).Interior.ColorIndex = -4142 Then 
       shouldHideRow = False 
       Exit For 
      End If 
     Next 

     If shouldHideRow Then 
      'drop into here if all cells in a row were white 
      Sheet2.Cells(currentRow, currentColumn).EntireRow.Hidden = True 
     End If 
    Next 
End Sub 

BEFORE

enter image description here

AFTER

enter image description here

+0

我很抱歉,如果我的措辭有點難以理解。我需要的是相反的。 我需要留下彩色的行。白色的必須隱藏。 – user2214690 2013-03-27 16:23:06

+0

我可能只是誤解:)對原始答案做出了正確的更改,現在應按照您的要求進行操作。 – Sam 2013-03-27 17:00:03

+1

謝謝! 問題:如果A列中沒有數據輸入,則該宏不會在該行中運行。此外,如果在彩色單元中沒有數據輸入 - 宏將隱藏它。 這個宏也需要我手動點擊才能運行。有沒有辦法讓它在開始時自動運行? 有沒有辦法讓它變成動態的? IE:由於這張紙與另一張紙相連,如果另一張紙上的數據強制一個單元在Sheet2上改變顏色 - 它可以自動取消隱藏嗎? – user2214690 2013-03-27 17:48:27

0
row = 1  
do 
    flag = false 
    col = 1 
    do 
    .cells(row,col).select 
    if selection.interior.colorindex <> vbWhite then flag = true 
    loop until (col = lastcol) or (flag = true) 
    if flag then 
    rows(row).delete 
    row = row - 1 
    end if 
    row = row + 1 
loop until row = lastrow 
+1

OP想隱藏,而不是刪除。 – Sam 2013-03-27 13:13:07