好的,我正在尝试从Column
中找到一个值。此Column
约有values
。虽然找到价值并不是一个问题,例如" Lassie"并且发现它位于Cell A301
上,这就产生了一个问题:我需要检查Row
Lassie所在的value
(这里是301)并搜索是否有其他标准满足。
我需要这样的东西:
CellA301
中的值为" Sales Revenue,Net`和,CellK301 = Start_Period
CellL301 = End_Period
CellM301 = ""
和CellN301 = ""
以及CellO301 = ""
和CellP301 = ""
以及CellQ301 = ""
如果满足所有这些条件,我需要copy
CellH301
和paste
另一个Sheet
技巧是我无法首先搜索CellA301
,如果它等于"销售收入,净额"执行所有其他测试,因为A列中有许多单元格,其中填充了字符串值" Sales Revenue,Net"只有完全符合上述条件的才是正确的。
修改
Sub wussss()
''disable autofilter in case it's already enabled'
ThisWorkbook.Sheets("31_December_2010").AutoFilterMode = False
With ThisWorkbook.Sheets("31_December_2010").Range("A1:AZ1")
'set autofilter'
.AutoFilter Field:=1, Criteria1:="Sales Revenue, Net"
.AutoFilter Field:=11, Criteria1:="1/1/2010"
.AutoFilter Field:=12, Criteria1:="12/31/2010" 'x
.AutoFilter Field:=13, Criteria1:=""
.AutoFilter Field:=14, Criteria1:=""
.AutoFilter Field:=15, Criteria1:="" 'X IT FAILS HERE
.AutoFilter Field:=16, Criteria1:=""
.AutoFilter Field:=17, Criteria1:=""
.AutoFilter Field:=18, Criteria1:=""
.AutoFilter Field:=19, Criteria1:=""
.AutoFilter Field:=20, Criteria1:=""
.AutoFilter Field:=21, Criteria1:=""
.AutoFilter Field:=22, Criteria1:=""
.AutoFilter Field:=23, Criteria1:=""
.AutoFilter Field:=24, Criteria1:=""
.AutoFilter Field:=25, Criteria1:=""
.AutoFilter Field:=26, Criteria1:=""
.AutoFilter Field:=27, Criteria1:=""
.AutoFilter Field:=28, Criteria1:=""
.AutoFilter Field:=29, Criteria1:=""
.AutoFilter Field:=30, Criteria1:=""
End With
With ThisWorkbook.Sheets("Sheet1")
On Error Resume Next
Set Rng = .Range("A2:A" & .Rows.Count).Rows.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
End With
If Not Rng Is Nothing Then
MsgBox Rng.Row ' returns first visible row number
End If
ThisWorkbook.Sheets("Sheet1").AutoFilterMode = False 'disable autofilter'
End Sub
答案 0 :(得分:1)
作为评论的后续内容,此代码有效:
Sub test()
Dim i As Byte
Dim rng As Range
With ThisWorkbook.Sheets("31_December_2010")
'disable autofilter in case it's already enabled'
.AutoFilterMode = False
With .Range("A1:AZ1")
.Value = .Value
'set autofilter'
.AutoFilter Field:=1, Criteria1:="Sales Revenue, Net"
.AutoFilter Field:=11, Criteria1:="1/1/2010"
.AutoFilter Field:=12, Criteria1:="12/31/2010" 'x
For i = 13 To 30
.AutoFilter Field:=i, Criteria1:=""
Next i
End With
On Error Resume Next
Set rng = .Range("A2:A" & .Rows.Count).Rows.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not rng Is Nothing Then MsgBox rng.Row ' returns first visible row number
.AutoFilterMode = False 'disable autofilter'
End With
End Sub
还有一点注意事项:
您只能对已使用的工作表部分应用AutoFilter
。例如。如果UsedRange
的最后一列为M
,则在行.Range("A1:AZ1").Autofilter
中,Excel仅对A1:M1
进行过滤。不过上面代码的.Value = .Value
部分,明确地将A1:AZ1
添加到工作表的已使用部分。