2012-04-25 204 views
2

我有一個已經完成或沒有完成的任務的excel文件,用列中的是或否表示。最終,我對不同列中的數據感興趣,但我想設置代碼,以便忽略已完成任務的那些行。到目前爲止,我已經定義了包含yes/no的列範圍,但我不知道在這個範圍上運行哪個命令。我想我要基於列C的值從另一個範圍的值中定義一個範圍

Option Explicit 

Sub Notify() 
    Dim Chk As Range 
    Dim ChkLRow As Long 
    Dim WS1 As Worksheet 

    On Error GoTo WhatWentWrong 

    Application.ScreenUpdating = False 

    '--> If the text in column C is Yes then Ignore (CountIF ?) 
    '--> Find last cell in the column, set column C range as "Chk" 

    Set WS1 = Sheets("2011") 

    With WS1 
     ChkLRow = .Range("C" & Rows.Count).End(xlUp).Row 
     Set Chk = .Range("C1:C" & ChkLRow) 
    End With 

    '--> Else Check date in column H 
    '--> Count days from that date until today 
    '--> Display list in Message Box 
Reenter: 
    Application.ScreenUpdating = True 
    Application.DisplayAlerts = True 
    Exit Sub 
WhatWentWrong: 
    MsgBox Err.Description 
    Resume Reenter 
    Application.ScreenUpdating = True 
End Sub 

難道也許更容易簡單地定義基於C列的值一個範圍,而不是首先定義列C的範圍內定義新範圍然後重新定義它?

感謝

+0

如何使用自動過濾器,然後獲得可見範圍?你需要使用VBA嗎?您也可以使用公式來獲取說「不」的總天數。 – 2012-04-25 20:07:09

+0

我試圖達到的最終結果是顯示一個消息框,供用戶根據請求顯示尚未完成任務的天數。我不想讓完成的任務始終處於隱藏狀態,並且最終用戶無法自行使用過濾器,因此使用VBA可以在請求時爲他們執行過濾。 – 2012-04-25 20:12:44

+0

所以你想要Col H的未完成日總數? – 2012-04-25 20:14:30

回答

3

是H列有任務「到達」的日期,我想從然後顯示計數爲當前日期。這些任務由列A中的4位數代碼標識。我設想消息框中說明任務'1234'在xx天內未處理。 - 阿利斯泰爾堰1分鐘前

這是你想什麼呢?爲了可視化目的添加了Col I。否則它不具有任何意義。

Option Explicit 

Sub Notify() 
    Dim WS1 As Worksheet 
    Dim Chk As Range, FltrdRange As Range, aCell As Range 
    Dim ChkLRow As Long 
    Dim msg As String 
    On Error GoTo WhatWentWrong 

    Application.ScreenUpdating = False 

    Set WS1 = Sheets("2011") 

    With WS1 
     ChkLRow = .Range("C" & Rows.Count).End(xlUp).Row 

     '~~> Set your relevant range here 
     Set Chk = .Range("A1:H" & ChkLRow) 

     '~~> Remove any filters 
     ActiveSheet.AutoFilterMode = False 

     With Chk 
      '~~> Filter, 
      .AutoFilter Field:=3, Criteria1:="NO" 
      '~~> Offset(to exclude headers) 
      Set FltrdRange = .Offset(1, 0).SpecialCells(xlCellTypeVisible) 
      '~~> Remove any filters 
      ActiveSheet.AutoFilterMode = False 

      For Each aCell In FltrdRange 
       If aCell.Column = 8 And _ 
       Len(Trim(.Range("A" & aCell.Row).Value)) <> 0 And _ 
       Len(Trim(aCell.Value)) <> 0 Then 
        msg = msg & vbNewLine & _ 
          "Task " & .Range("A" & aCell.Row).Value & _ 
          " outstanding for " & _ 
          DateDiff("d", aCell.Value, Date) & "days." 
       End If 
      Next 
     End With 
    End With 

    '~~> Show message 
    MsgBox msg 
Reenter: 
    Application.ScreenUpdating = True 
    Exit Sub 
WhatWentWrong: 
    MsgBox Err.Description 
    Resume Reenter 
End Sub 

快照

enter image description here

+0

絕對完美!在您花費的時間裏,我所管理的就是定義過濾後的可見範圍! :) – 2012-04-25 20:36:59

+1

'Len'的作用是什麼?與公式一樣,計算字符串中的字符數? – 2012-04-25 20:42:13

+0

是的。我想要做的是檢查A欄和H欄中的相應單元格是否爲空 – 2012-04-25 20:44:14

0

爲什麼不蠻力它。

Dim r_table as Range, i as Integer, N as Integer 
' Start from the top 
Set r_table = Sheets("2011").Range("C1") 
' Find the last entry on column C and count the # of cells 
N = Sheets("2011").Range(r_table, r_table.End(xlDown)).Rows.Count 
Dim table_values() as Variant 
' This will transfer all the values from the spreadsheet into an VBA array 
' and it works super fast. Access values with A(row,col) notation. 
table_values = r_table.Resize(N, 5).Value2 ' No. of columns is 5 ? 

For i=1 to N 
    If table_values(i,1)="Yes" Then 'Check Column C 
    Else 
     ... table_values(i,5) ' Column H 

    End if 
Next i 
MsgBox .... 

這將是超級快,在屏幕上無閃爍。

相關問題