0

我正在努力编写代码 - 下面的查询请帮助任何人编写它。

  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 循环,但是在找到第一行之后,它没有找到第二行值,它再次从第一行开始

请帮我解决上面的代码。

谢谢&问候, 芭莎

4

2 回答 2

0

我不知道为什么你必须通过每个值。
你已经习惯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

结束子

顺便说一句,这是基于这篇文章,您可能还想查看它以改进编码。
这是一本好书。希望这可以帮助。

于 2013-10-21T05:57:05.413 回答
0

您正在寻找.FindNext功能。尝试这样的事情:(请注意,您可能需要稍微修改此代码以适合您的特定情况。)

Sub UseFindNext()

Dim TestDataSheet As Worksheet
Dim FoundCell1 As Range
Dim DateToday As Date
Dim firstAddress As String
Dim x As Long
Dim CountTestData As Long
Dim CountTestDataAftFilter As Long

x = 1

Set TestDataSheet = ActiveWorkbook.Worksheets(x)

CountTestData = TestDataSheet.Range("A" & Rows.count).End(xlUp).Row
Range("A10").AutoFilter Field:=14, Criteria1:=">=" & 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.Address
Do
    'Do whatever you're looking to do with each cell here. For example:
    Debug.Print FoundCell1.Value
Loop While Not FoundCell1 Is Nothing And FoundCell1.Address <> firstAddress

End Sub
于 2013-10-21T05:15:42.320 回答