How to find max and min date in a range of another sheet?

前端 未结 2 1475
一个人的身影
一个人的身影 2020-12-19 16:23

I am writing VBA code to find the minimum and maximum dates in a Range. When I execute it, I get an error:

Run-time error \'1004\': Application-define

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-19 16:55

    You are telling range that its parent is Sheets("Data") but not cells. For all intents and purposes you wanted a range from Data!E2:Schedule!E99.

    Sub GenerateSheet()
    
        Dim i, r, numAssignments As Integer
        Dim ssrRng, DestRange As Range
        Dim StartDate, EndDate, d As Date
    
        numAssignments = Sheets("Data").Range("A1048576").End(xlUp).Row - 1
        Sheets("Schedule").Select
    
        with Sheets("Data")
            EndDate = WorksheetFunction.Max(.Range(.Cells(2, 8), .Cells(numAssignments, 8)))
            StartDate = WorksheetFunction.Min(.Range(.Cells(2, 5), .Cells(numAssignments, 5)))
        end with
    
    End Sub
    

    Using the With Sheets("Data") tells everything inside that block that is prefaced with a period (aka . or full stop) that its parent is Sheets("Data").

提交回复
热议问题