从另一个范围的值中定义一个范围
我有一个已经完成或没有完成的任务的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的范围内定义新范围然后重新定义它?
感谢
是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
快照
绝对完美!在您花费的时间里,我所管理的就是定义过滤后的可见范围! :) – 2012-04-25 20:36:59
'Len'的作用是什么?与公式一样,计算字符串中的字符数? – 2012-04-25 20:42:13
是的。我想要做的是检查A栏和H栏中的相应单元格是否为空 – 2012-04-25 20:44:14
为什么不蛮力它。
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 ....
这将是超级快,在屏幕上无闪烁。
如何使用自动过滤器,然后获得可见范围?你需要使用VBA吗?您也可以使用公式来获取说“不”的总天数。 – 2012-04-25 20:07:09
我试图达到的最终结果是显示一个消息框,供用户根据请求显示尚未完成任务的天数。我不想让完成的任务始终处于隐藏状态,并且最终用户无法自行使用过滤器,因此使用VBA可以在请求时为他们执行过滤。 – 2012-04-25 20:12:44
所以你想要Col H的未完成日总数? – 2012-04-25 20:14:30