Excel 根据日期从&;向下列出所有行;使用VBA进行日期转换

Excel 根据日期从&;向下列出所有行;使用VBA进行日期转换,excel,vba,Excel,Vba,我一直在努力解决这个问题 我使用此示例数据来获取介于Date From和Date to之间的行: 表1 此工作表包含日期从:和日期到:单元格,这些单元格将自动显示以下结果 这是我的Sheet2,从中提取数据 这是我当前的VBA代码 Sub FinalData() Dim lastrow As Long Dim count As Integer Dim p As Integer Dim x As Integer

我一直在努力解决这个问题

我使用此示例数据来获取介于Date From和Date to之间的行:

表1

此工作表包含日期从:和日期到:单元格,这些单元格将自动显示以下结果

这是我的Sheet2,从中提取数据

这是我当前的VBA代码

   Sub FinalData()

        Dim lastrow As Long
        Dim count As Integer
        Dim p As Integer
        Dim x As Integer

        lastrow = Sheets("Sheet2").Cells(rows.count, 1).End(xlUp).row
         Sheets("Sheet1").Range("A5:C1000").ClearContents

          count = 0
          p = 5
          For x = 2 To lastrow

          If Sheets("Sheet2").Range("C2:C100") >= Sheets("Sheet1").Cells(1, 2) AND Sheets("Sheet2").Range("C2:C100") <= Sheets("Sheet1").Cells(2, 2) Then

          Sheets("Sheet1").Cells(p, 1) = Sheets("Sheet2").Cells(x, 1)

          Sheets("Sheet1").Cells(p, 2) = Sheets("Sheet2").Cells(x, 2)

          Sheets("Sheet1").Cells(p, 3) = Sheets("Sheet2").Cells(x, 3)

          p = p + 1
          count = count + 1

          End If

          Next x

          MsgBox " The number of data found for this Area is " & " " & count
    End Sub         
子财务数据()
最后一排一样长
将计数设置为整数
作为整数的Dim p
作为整数的Dim x
lastrow=工作表(“Sheet2”)。单元格(rows.count,1)。结束(xlUp)。行
纸张(“纸张1”)。范围(“A5:C1000”)。清晰目录
计数=0
p=5
对于x=2到最后一行

如果Sheets(“Sheet2”).Range(“C2:C100”)>=Sheets(“Sheet1”).单元格(1,2)和Sheets(“Sheet2”).Range(“C2:C100”),则问题在于您试图将一个单元格范围与两个单个单元格进行比较

未经测试:

   Sub FinalData()

        Dim lastrow As Long
        Dim count As Long
        Dim p As Long
        Dim x As Long, dt
        Dim wsReport As Worksheet, wsData As Worksheet

        Set wsReport = ThisWorkbook.Sheets("Sheet1")
        Set wsData = ThisWorkbook.Sheets("Sheet2")

        lastrow = wsData.Cells(Rows.count, 1).End(xlUp).Row
        wsReport.Range("A5:C1000").ClearContents

        count = 0
        p = 5

        For x = 2 To lastrow
            dt = wsData.Cells(x, "C")
            If dt >= wsReport.Cells(1, 2) And dt <= wsReport.Cells(2, 2) Then
                With wsReport
                    .Cells(p, 1) = wsData.Cells(x, 1)
                    .Cells(p, 2) = wsData.Cells(x, 2)
                    .Cells(p, 3) = wsData.Cells(x, 3)
                End With

                p = p + 1
                count = count + 1
            End If
        Next x

        MsgBox " The number of data found for this Area is " & " " & count
    End Sub
子财务数据()
最后一排一样长
不算长
变暗p为长
尺寸x等于长度,dt
将wsReport设置为工作表,将wsData设置为工作表
设置wsReport=thiswoolk.Sheets(“Sheet1”)
设置wsData=ThisWorkbook.Sheets(“Sheet2”)
lastrow=wsData.Cells(Rows.count,1).End(xlUp).Row
wsReport.Range(“A5:C1000”).ClearContents
计数=0
p=5
对于x=2到最后一行
dt=wsData.Cells(x,“C”)
如果dt>=wsReport.Cells(1,2)和dt