Excel 使用for循环自动筛选后未获取所有行的值
我正在努力编写下面的代码查询,请帮助任何人编写它Excel 使用for循环自动筛选后未获取所有行的值,excel,vba,Excel,Vba,我正在努力编写下面的代码查询,请帮助任何人编写它 TestDataSheetName = ActiveWorkbook.Worksheets(x).Name ActiveWorkbook.Worksheets(x).Activate CountTestData = ActiveWorkbook.Worksheets(x).Range("A" & Rows.Count).End(xlUp).Row Range("A10").Select Range("A10").AutoFil
TestDataSheetName = ActiveWorkbook.Worksheets(x).Name
ActiveWorkbook.Worksheets(x).Activate
CountTestData = ActiveWorkbook.Worksheets(x).Range("A" & Rows.Count).End(xlUp).Row
Range("A10").Select
Range("A10").AutoFilter
Selection.AutoFilter Field:=14, Criteria1:=">=" & DateToday
ActiveWorkbook.Worksheets(x).Activate
CountTestDataAftFilter = ActiveWorkbook.Worksheets(x).Range("A1", Range("A65536").End(xlUp)).SpecialCells(xlCellTypeVisible).Count
MsgBox CountTestDataAftFilter
For w = 10 To CountTestDataAftFilter
Set Foundcell1 = ActiveWorkbook.Worksheets(x).Cells.Find(What:=DateToday, After:=[ActiveCell], _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
LookIn:=xlValues, LookAt:=xlPart, MatchCase:=True)
Next
'使用今天的日期筛选后,我得到了5行今天的日期,我编写了for循环以获取所有行值,但在找到第一行后,它没有找到第二行值,并且再次从第一行开始
请帮我查一下上面的代码
谢谢和问候,
Basha您正在寻找
.FindNext
函数。尝试以下方法:(请注意,您可能需要稍微修改此代码以适合您的特定情况。)
Sub-UseFindNext()
将测试数据表设置为工作表
Dim FoundCell1 As范围
Dim Date今天作为日期
将第一个地址设置为字符串
暗x等长
Dim CountTestData尽可能长
Dim CountTestDataAftFilter尽可能长
x=1
设置TestDataSheet=ActiveWorkbook.Worksheets(x)
CountTestData=TestDataSheet.Range(“A”和Rows.count).End(xlUp).Row
范围(“A10”)。自动筛选字段:=14,标准1:=“>=”&DateToday
CountTestDataAftFilter=TestDataSheet.Range(“A1”,Rows.count).End(xlUp)).SpecialCells(xlCellTypeVisible).count
Set FoundCell1=TestDataSheet.Cells.Find(What:=DateToday,After:=TestDataSheet.Range(“A10”)_
SearchOrder:=xlByRows,SearchDirection:=xlNext_
LookIn:=xlValues,LookAt:=xlPart,MatchCase:=True)
firstAddress=FoundCell1.地址
做
“对这里的每个细胞,你想做什么就做什么。例如:
Debug.Print FoundCell1.Value
非FoundCell1时循环为Nothing,FoundCell1.Address firstAddress
端接头
我不知道为什么要检查每个值您已经使用了
AutoFilter
来获取所需的数据但这里有另一种可能对你有用的方法
Sub test()
Dim ws As Worksheet
Dim wb As Workbook
Dim DateToday As String 'i declared it as string for the filtering
Dim rng, cel As Range
Dim lrow As Long
Set wb = ThisWorkbook
Set ws = wb.Sheets(x)
DateToday = "Put here whatever data you want" 'put value on your variable
With ws
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("N10:N" & lrow).AutoFilter Field:=1, Criteria1:=DateToday
'I used offset here based on the assumption that your data has headers.
Set rng = .Range("N10:N" & lrow).Offset(1, 0).SpecialCells(xlCellTypeVisible)
'here you can manipulate the each cell values of the currently filtered range
For Each cel In rng
cel.EntireRow 'use .EntireRow to get all the data in the row and do your stuff
Next cel
.AutoFilterMode = False
End With
端接头
顺便说一句,这是基于您可能希望检查的帖子,以改进编码。这是一本好书。希望这能有所帮助。为延迟响应道歉感谢我得到的每一个解决方案
Sub test()
Dim ws As Worksheet
Dim wb As Workbook
Dim DateToday As String 'i declared it as string for the filtering
Dim rng, cel As Range
Dim lrow As Long
Set wb = ThisWorkbook
Set ws = wb.Sheets(x)
DateToday = "Put here whatever data you want" 'put value on your variable
With ws
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("N10:N" & lrow).AutoFilter Field:=1, Criteria1:=DateToday
'I used offset here based on the assumption that your data has headers.
Set rng = .Range("N10:N" & lrow).Offset(1, 0).SpecialCells(xlCellTypeVisible)
'here you can manipulate the each cell values of the currently filtered range
For Each cel In rng
cel.EntireRow 'use .EntireRow to get all the data in the row and do your stuff
Next cel
.AutoFilterMode = False
End With